简体   繁体   中英

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. 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

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

CommentNumberDelegate.qml: small version

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. 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.

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. 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.

And that pointer i used to connect the signal inside the delegate to a signal in 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()));
}

so basically, all you need to do is:

Call the showcommentsbutton from QML. This will emit the signal. And from QML you can catch it like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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