QML-将信号从c++连接到QML

QML - connect a signal from c++ to qml

本文关键字:连接 QML c++ 信号 QML-      更新时间:2023-10-16

我有一堆嵌套列表,链的最深列表的高度为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")

现在,我不确定这是否是正确的方式,但它对我来说很有用。