QML元素ID访问从C 访问
qml element id access from c++
在C 方面我写了此代码
:::::::::::::
QMetaObject::invokeMethod(rootObject,"changeText",Q_ARG(QVariant,"txt1"),
Q_ARG(QVariant,"hello"))
在qml侧我写了这篇
Text {
id: txt1
text: "hi"
}
function changeText(id,str){
id.text=str
}
ChangEtext函数在QML侧起作用,但是当我从C 侧调用它时,它不起作用。我认为CPP侧方法将" TXT1"发送为QString,因此ChangEtext函数不起作用。
你能告诉我我该怎么做?
从C 更改QML对象的属性的正确方法是在C 中获取该对象,而不是Call SetProperty()方法。例子:QML:
Rectangle
{
id: container
width: 500; height: 400
Text {
id: txt1
objectName: "text1"
text: "hi"
}
}
请注意,您必须添加用于获取孩子的对象名称属性。在此示例中,矩形是rootObject。然后在C 中:
QObject *rootObject = dynamic_cast<QObject*>(viewer.rootObject());
QObject *your_obj = rootObject->findChild<QObject*>("text1");
your_obj->setProperty("text", "500");
您可以将其压缩到这样的一行呼叫:
viewer.rootObject()->findChild<QObject*>("text1")->setProperty("text", "You text");
一种替代方法是使用您之前的方法,但将对象名称命名为 ChangEtext 方法,并通过主要对象的孩子进行迭代,直到找到您感兴趣的一种方法:
Rectangle {
id: container
width: 500; height: 400
Text {
id: txt1
objectName: "text1"
text: "hi"
}
function changeText(objectName,str){
for (var i = 0; i < container.children.length; ++i)
if(container.children[i].objectName === objectName)
{
container.children[i].text = str;
}
}
}
相关文章:
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 用于访问容器<T>数据成员的正确 API
- 访问者访问变体并返回不同类型时出错
- 尝试通过多个向量访问变量时,向量下标超出范围
- 无法访问嵌套类.类的使用无效
- 写入位置0x0000000C时发生访问冲突
- 我们可以访问一个不存在的联盟的成员吗
- C++从另一个类访问公共静态向量的正确方法是什么
- 我的简单if-else语句是如何无法访问的代码
- 一种有效的数据结构,用于按 ID 访问和查找加权随机项
- 通过 id 访问 QML 插槽中的嵌套对象(从 c++ 调用)
- OpenMP:当我按线程 ID 访问共享变量时,我是否需要一个关键部分
- 使用宏访问指向数据成员的指针会导致"error: expected unqualified-id before ‘*’ token"
- QML元素ID访问从C 访问
- 具有可移动节点、可访问属性和可靠ID的C++图
- 通过指针访问类的成员时"unqualified-id"编译器错误
- Qt:如何访问在主窗口上按id拖动的元素
- 仅使用一个用户id和密码访问数据库