简体   繁体   English

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

[英]QML - connect a signal from c++ to qml

I have a bunch of nested lists and the deepest list of the chain has 0 height. 我有一堆嵌套列表,链中最深的列表的高度为0。 So when a user clicks a button, the list should expand. 因此,当用户单击一个按钮时,该列表将展开。

My problem is, that i cannot get a signal to the delegate of the list so that it knows when to expand. 我的问题是,我无法向列表的代表发出信号,以便它知道何时扩展。 And i have tried quite a lot of stuff. 而且我尝试了很多东西。

One method i have used allready with succes is failing me because it cannot find the correct object 我已经成功使用allready的一种方法使我失败,因为它找不到正确的对象

view->setSource(QUrl(QStringLiteral("qrc:/content/CommentNumberDelegate.qml")));
QObject *obj3 = view->rootObject()->findChild<QObject*>("commentNumberDelegate");
QObject::connect(&gather, SIGNAL(showCommentsButtonClicked()), obj3, SIGNAL(showCommentsButtonClicked()));

This gives me the error: 这给了我错误:

QObject::connect: Cannot connect DataGather::showCommentsButtonClicked() to (null)::showCommentsButtonClicked()

And yes, i have correctly set the ObjectName property of the file that i am accessing(CommentNumberDelegate.qml). 是的,我已经正确设置了我正在访问的文件的ObjectName属性(CommentNumberDelegate.qml)。

CommentNumberDelegate.qml: small version 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()
}

Even though i have succesfully connected signals like this before, I cannot get this to work. 即使我以前已经成功连接了这样的信号,也无法使它正常工作。 This code works: 此代码有效:

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()));

When i use the debugger i can see that the QObject *obj3 is not being assigned correctly because i cannot access it. 当我使用调试器时,我可以看到QObject * obj3没有正确分配,因为我无法访问它。 When using the debugger on obj and ob2 (the working code), i can clearly see that the object is assigned correctly because i can access it with the debugger after assigning to it. 在obj和ob2(工作代码)上使用调试器时,我可以清楚地看到该对象已正确分配,因为在分配给对象之后,我可以使用调试器对其进行访问。

my question is: What could i be doing wrong here? 我的问题是:我在这里可能做错了什么?

any way i tried getting the object failed,, it remained null. 我尝试使对象失败的任何方式,都保持为空。

So i tried to see if in QML itself, the object would be available each time the delegate was completed. 因此,我尝试查看在QML本身中,每次委托完成后该对象是否可用。 So i did: 所以我做了:

delegate: CommentNumberDelegate {
    objectName: "commentNumberDelegateID"
    id: commentNumberDelegateID

    Component.onCompleted: {
    console.log("delegate completed = ", commentNumberDelegateID)
    gatherer.test123(commentNumberDelegateID) // this function passes the address to c++
}

output: 输出:

qml: delegate completed =  CommentNumberDelegate_QMLTYPE_2(0x2bfa5748, "commentNumberDelegateID")
qml: delegate completed =  CommentNumberDelegate_QMLTYPE_2(0x2bf78d38, "commentNumberDelegateID")

So as you can see the object is indeed available when the component completes. 因此,您可以看到组件完成时该对象确实可用。 So all i did was pass the address to a function called test123, which takes a QObject pointer as parameter. 所以我所做的就是将地址传递给一个名为test123的函数,该函数将QObject指针作为参数。

And that pointer i used to connect the signal inside the delegate to a signal in C++ 我曾经使用该指针将委托内部的信号连接到C ++中的信号

gatherer.h gatherer.h

class DataGather : public QObject
{
    ....
    Q_INVOKABLE void showCommentsButton() { emit showCommentsButtonClicked(); }
    Q_INVOKABLE void test123(QObject *obj);
    ....

    signals:
    void showCommentsButtonClicked();
}

gatherer.cpp gatherer.cpp

void DataGather::test123(QObject* obj)
{
    QObject::connect(this, SIGNAL(showCommentsButtonClicked()), obj, SIGNAL(showCommentsButtonClicked()));
}

so basically, all you need to do is: 因此,基本上,您需要做的是:

Call the showcommentsbutton from QML. 从QML调用showcomments按钮。 This will emit the signal. 这将发出信号。 And from QML you can catch it like this: 从QML您可以像这样捕获它:

commentNumberDelegate.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")

now, i am not sure if this is the right way but it did the trick for me. 现在,我不确定这是否是正确的方法,但是它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM