根据中心点从最小到最大对QGraphicsItems QList进行排序
Sort QGraphicsItems QList based on center point min to max
如何根据QGraphicsItems的边界Rect中心点对其列表进行排序?我希望列表从最小Y值到最大Y值进行排序。
感谢
QList<QGraphicsItem*> validItems;
foreach (QGraphicsItem* item, items) {
if (!item) continue;
if (item->type() == NexusBlockItem::Type) {
int nodeCenterY = item->pos().y() + (item->boundingRect().height()/2.0);
}
}
Qt为此提供了一个很好的功能:qSort()
bool itemLess(const QGraphicsItem* item1, const QGraphicsItem* item2)
{
return item1->sceneBoundingRect().center().y() < item2->sceneBoundingRect().center.y();
}
qSort(validItems.begin(), validItems.end(), itemLess);
注意1:您可以使用std::sort()
而不是qSort()
。
注意2:您可以使用函子或lambda来比较项,而不是itemLess()
函数。
注意3:qSort()
和std::sort()
都使用快速排序算法。
编辑
正如vicrucann所提到的,Qt文档建议使用std::sort()
而不是qSort()
。
相关文章:
- 如何检查QList中是否存在值
- 访问类 QList 指针的成员
- C++ template for QList
- QGraphicsItems not showing up QGraphicsScene
- 如何使用 QSortFilterProxyModel::sort 对 Qlist 中的数据进行排序
- QT 如何将 Qlist 传递给类构造函数
- 尝试在 qml 中访问结构成员的 QList
- 如何将 QString 的 QList 分配给列表视图?
- Qt5 - 如何将"QList<T> *"指针作为信号参数传递,并在完成后将其删除?
- 将 QList 转换为 QStringList 以插入到 QComboBox 中
- 如何将QGraphicsItem QList保存到文件中,然后读取它?Qt c++
- 从 json 字符串将 QVariant 转换为 QList<MyClass>
- 如何在 c++98 中通过特定字段在 QList 中查找对象?
- 在 QML 中读取 QList<QPoint>
- 具有QList功能的QT应用程序<QString>"append"
- 如何将对象/指针正确存储到 Qlist 中
- 为什么 std::sort 在使用 QList 时崩溃?
- QList::operator[]中的断言失败<T>:QJoysticks 中的"index out of range"
- 为什么我不能像使用 std::string::size_type 那样使用 QList::size_type?(模板参数错误)
- 根据中心点从最小到最大对QGraphicsItems QList进行排序