如何在C 方面从头开始创建新的Qquickitem副本,其属性与现有的属性相同



我从c 侧获取了一个QQuickItem

QQuickItem * my_item = qmlEngine->rootObjects()[0]->findChild<QQuickItem*>("ItemObjectName");

my_item有效&amp;将所有属性设置在其上。

方案
我有 2个Windows ,需要在更改上绘制此QQuickItem。我想将此my_item渲染到另一个窗口。只要我将my_item的父母设置为另一个窗口

,这一切都很好。
// the_other_window is a QQuickWindow
QQuickWindow * the_other_window;
// here I set parent
my_item->setParentItem(the_other_window->contentItem());

这要求我再次进行setParentItem,然后再回到my_item的原始窗口,否则在原始窗口上不可见。这是有效的,但给了我不必要的依赖。相反,我正在尝试创建QQuickItem&amp;对此进行setParentItem。通过这样的复制:

QQuickItem * item_copy = new QQuickItem(my_item);

问题:我想使item_copymy_item完全相同。我了解到QQuickItem不可复制。因此,要将my_item的所有属性设置为item_copy。因此从头开始重新创建my_item。我怎样才能做到这一点?我应该主要复制哪些属性?宽度,高度,x,y&amp;还有什么?

是否有一种方法将所有有效属性复制到本新属性的情况下,而无需一个一个地复制每个有效属性?

要复制您必须使用metaobject()的所有属性,通过此,我们获得了属性,我们可以将其复制,如下所示。注意:并非所有属性都可以编写,因为我们使用方法isWritable()

检查
QQuickItem *copyQQuickItem(QQuickItem *item){
    QQuickItem *item_Copy = new QQuickItem;
    const QMetaObject *mo = item->metaObject();
    for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i){
        if(mo->property(i).isWritable()){
            const char* name = mo->property(i).name();
            item_Copy->setProperty(name, item->property(name));
        }
    }
    return item_Copy;
}

示例:

QQuickItem * my_item = engine.rootObjects()[0]->findChild<QQuickItem*>("ItemObjectName");
QQuickItem *item_copy = copyQQuickItem(my_item);
const QMetaObject *mo = my_item->metaObject();
for (int i = mo->propertyOffset(); i < mo->propertyCount(); ++i){
    if(mo->property(i).isWritable()){
        const char* name = mo->property(i).name();
        qDebug()<<name<<my_item->property(name)<<item_copy->property(name);
        Q_ASSERT(my_item->property(name)==item_copy->property(name));
    }
}

输出:

parent QVariant(QQuickItem*, QQuickRootItem(0x55975cfdf520)) QVariant(QQuickItem*, QQuickRootItem(0x55975cfdf520))
x QVariant(double, 0) QVariant(double, 0)
y QVariant(double, 0) QVariant(double, 0)
z QVariant(double, 0) QVariant(double, 0)
width QVariant(double, 100) QVariant(double, 100)
height QVariant(double, 100) QVariant(double, 100)
opacity QVariant(double, 1) QVariant(double, 1)
enabled QVariant(bool, true) QVariant(bool, true)
visible QVariant(bool, true) QVariant(bool, true)
state QVariant(QString, "") QVariant(QString, "")
baselineOffset QVariant(double, 0) QVariant(double, 0)
clip QVariant(bool, false) QVariant(bool, false)
focus QVariant(bool, false) QVariant(bool, false)
activeFocusOnTab QVariant(bool, false) QVariant(bool, false)
rotation QVariant(double, 0) QVariant(double, 0)
scale QVariant(double, 1) QVariant(double, 1)
transformOrigin QVariant(int, 4) QVariant(int, 4)
smooth QVariant(bool, true) QVariant(bool, true)
antialiasing QVariant(bool, false) QVariant(bool, false)
implicitWidth QVariant(double, 0) QVariant(double, 0)
implicitHeight QVariant(double, 0) QVariant(double, 0)

相关内容

  • 没有找到相关文章

最新更新