首页 文章

如何正确使用QQuickItem :: stackBefore()重新排序GridLayout中的项目?

提问于
浏览
3

我有很多混合QML和C的经验,但是最近遇到了麻烦:我在QML代码中有GridLayout项 . 最初我用C来填充QQuickItems(实际上包含图标的矩形) . 这适用于渲染,包括大小调整,包装QML定义的列:属性等 .

现在我需要基于用户交互(鼠标点击矩形)在准确位置插入新的矩形(实际上就在单击的Rectangle之前) . 我对MouseEvent传播没有任何问题,我甚至能够将任何大量的参数(无论是值或指向项目的指针)传递到我要处理的方法中 . 我只是找不到“c措辞”来使矩形真正重新排列 .

我使用以下代码:

QQuickItem *icon = place(parent);
icon->stackBefore(insertBefore);
parent->update();   //(1)
GUI::programWindow->update(); //(2)
qDebug() << "\tCHILDS:" << parent->childItems(); //(3)

parent是指向从QML代码传递的GridLayout项的指针(已验证它指向预期的位置) . place(parent)是我的方法,它根据这个对象加载正确的图标,生成周围的矩形并返回指向它的指针(即*图标实际上是矩形) . insertBefore是指向同一GridLayout内的其他矩形的指针(通过我的onClick处理程序传递自定义信号) . 双重检查它是适当的GridLayout项的子项 .

现在我希望,那个首先被生成为最后一个GridLayout子节点的Rectangle,在预定的其他Rect和GridLayout(知道,视觉子命令改变了)之前将“快速点亮”传送到将重新绘制到屏幕的新布局 . 事实上,我面向新创建的Rect作为最后一个GridLayout项目 .

有趣的是,第(3)行的输出显示正确的,即儿童的重新排序顺序 . 因此,parent-> childItems()的输出顺序与屏幕上显示的顺序不同 . 注释(1)和(2)的行是我绝望的尝试使GridLayout重绘,但没有出现明显的变化 .

有没有人有重新排序GridLayout孩子的经验?是否有其他更合适的方法来调用而不是stackBefore()?或者我错过了一些愚蠢的,立即可见的东西:)?

提前感谢任何线索 .

1 回答

  • 0

    你正在触发 QQuickItem::update() ,但对于重新安排调用QQuickItem::polish()的孩子应该就够了 . 根据Scene Graph and Rendering doc, QQuickItem::updatePolish() 是通过 QQuickItem::polish() 调用 final touch-up of items before they are rendered 来安排的 . 但 polish 事件应安排在场景中最顶级的QQuickItem而不仅仅是父项目 .

    此外,在使用QQuickItem::stackBeforeQQuickItem::stackAfter时,您需要知道两个重新排序项应该是同一父QQuickItem实例的子项 .

    如果您要将 QQuickItem 插入子项列表的末尾,因为它是新添加项目的默认位置,您可以使用QQuickItem::setParentItem .

    上面的摘要,最终实现可能如下所示:

    bool insertChild(QQuickItem* item, int position, QQuickItem* parent, QQuickItem* topmostItem) {
        if (!item || !parent || !topmostItem)
            return false;
        QList<QQuickItem*> children = parent->childItems();
        if (children.size() && children.size() > position) {
            QQuickItem* nextItem = children.at(position);
            item->setParentItem(parent);
            item->stackBefore(nextItem);
        } else {
            item->setParentItem(parent);
        }
        topmostItem->polish();
        return true;
    }
    

    作为QQuickItem::stackBeforeQQuickItem::stackAfter的替代,可以单独使用QQuickItem::setParentItem,但在这种情况下复杂性是线性的:

    ...
    QList<QQuickItem*> children = parent->childItems();
    // Remove all children from parent from insert position and to the end
    for (int index = position; index < children.size(); ++index) {
        children[index]->setParentItem(nullptr);
    }
    // Push back new QQuickItem into parent
    item->setParentItem(parent);
    // Push back into parent all previously temporary removed children
    for (int index = position; index < children.size(); ++index) {
        children[index]->setParentItem(parent);
    }
    topmostItem->polish();
    ...
    

相关问题