QML-将信号从c++连接到QML
QML - connect a signal from c++ to qml
我有一堆嵌套列表,链的最深列表的高度为0。因此,当用户单击按钮时,列表应该展开。
我的问题是,我无法向列表的委托人发送信号,使其知道何时展开。我已经尝试了很多东西。
我使用的一种方法allready with successs失败了,因为它找不到正确的对象
view->setSource(QUrl(QStringLiteral("qrc:/content/CommentNumberDelegate.qml")));
QObject *obj3 = view->rootObject()->findChild<QObject*>("commentNumberDelegate");
QObject::connect(&gather, SIGNAL(showCommentsButtonClicked()), obj3, SIGNAL(showCommentsButtonClicked()));
这给了我一个错误:
QObject::connect: Cannot connect DataGather::showCommentsButtonClicked() to (null)::showCommentsButtonClicked()
是的,我已经正确地设置了我正在访问的文件的ObjectName属性(CommentNumberDelegate.qml)
CommentNumberDelegate.qml:小型版
Rectangle {
id: root
objectName: "commentNumberDelegate"
//width: parent.width + 20
height: 0 //col1.childrenRect.height //Screen.height * 0.1 //300 //col1.childrenRect.height
clip: true
property alias delegateState: root.state
signal showCommentsButtonClicked()
}
尽管我以前成功地连接了这样的信号,但我无法让它发挥作用。此代码有效:
view->setSource(QUrl(QStringLiteral("qrc:/LoginPage.qml")));
QObject *obj = view->rootObject()->findChild<QObject*>("loginRectID");
QObject::connect(&gather, SIGNAL(loginSequenceFinished(QString)), obj, SIGNAL(loginSequenceFinished(QString)));
QObject *obj2 = view->rootObject()->findChild<QObject*>("mainRectID");
QObject::connect(&gather, SIGNAL(xmlFileCreationFinished()), obj, SIGNAL(xmlFileCreationFinished()));
当我使用调试器时,我可以看到QObject*obj3没有被正确分配,因为我无法访问它。当在obj和ob2(工作代码)上使用调试器时我可以清楚地看到对象被正确分配了,因为我可以在分配给它之后用调试器访问它。
我的问题是:我在这里做错了什么
我尝试获取对象的任何方法都失败了,它仍然为null。
所以我试着看看在QML本身中,每次委托完成时对象是否可用。所以我做了:
delegate: CommentNumberDelegate {
objectName: "commentNumberDelegateID"
id: commentNumberDelegateID
Component.onCompleted: {
console.log("delegate completed = ", commentNumberDelegateID)
gatherer.test123(commentNumberDelegateID) // this function passes the address to c++
}
输出:
qml: delegate completed = CommentNumberDelegate_QMLTYPE_2(0x2bfa5748, "commentNumberDelegateID")
qml: delegate completed = CommentNumberDelegate_QMLTYPE_2(0x2bf78d38, "commentNumberDelegateID")
因此,正如您所看到的,当组件完成时,对象确实是可用的。因此,我所做的只是将地址传递给一个名为test123的函数,该函数将QObject指针作为参数。
我用那个指针把委托内部的信号连接到C++中的信号
gatherer.h
class DataGather : public QObject
{
....
Q_INVOKABLE void showCommentsButton() { emit showCommentsButtonClicked(); }
Q_INVOKABLE void test123(QObject *obj);
....
signals:
void showCommentsButtonClicked();
}
gatherer.cpp
void DataGather::test123(QObject* obj)
{
QObject::connect(this, SIGNAL(showCommentsButtonClicked()), obj, SIGNAL(showCommentsButtonClicked()));
}
所以基本上,你所需要做的就是:
从QML调用showcomments按钮。这将发出信号。从QML你可以看到这样的:
commentNumberDelegate.qml:
Rectangle {
id: commentNumberDelegate
objectName: "commentNumberDelegate"
width: parent.width + 20
height: col1.childrenRect.height
clip: true
signal showCommentsButtonClicked()
onShowCommentsButtonClicked: console.log("showCommentsButtonClicked signal has been caught")
现在,我不确定这是否是正确的方式,但它对我来说很有用。
相关文章:
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- QT - 连接 Qml 按钮 单击到 Cpp 构造函数
- 连接 qml 拨号和 qml 中的滑块
- 可以在 QObject::connect() C++中连接 QML 对象现有信号?
- 销毁 qml 对象后的信号槽连接
- 将QAbstractTableModel与QML TableView连接起来
- QML和C 连接到信号
- 如何使用C 插槽连接QML信号
- QT:C 信号到QML连接
- 连接从 QML 到 Qt 的信号:var 到 QList<QString>
- QML组件与信号连接
- Qt-Qml连接到上下文属性的QObject属性的信号
- 当连接的 QML 属性更改时C++对象不会获取值
- 连接到来自 QML C++信号
- QObject::connect:在 c++ Qt 5.3 中连接 qml 信号时没有这样的信号
- 如何连接来自c++代码的嵌套qml项的信号
- 无法将 QML 组合框信号连接到插槽C++
- 级联导航窗格:连接到另一个qml文件的对象信号
- 动态连接 QML 中图形的节点 la Pd 或 Max(又名"patching"接口)
- 如何在qml上检查网络连接?