Using Quick to Interface with C++



  • Is there a way to represent a Quick object, such as a Button, in C++ as a class such that signals will go to that class when invoked on the Quick object ?



  • Sure, you can use the QMetaObject methods to introspect signals from any object constructed in QML and connect them to whichever signals/slots of QObjects constructed by you.

    You can also do it the other way around, if you expose those QObjects to QML and then do: Component.onCompleted: { myQmlObjectId.someSignalName.connect(someCppQObject.slotName) } for example.

    Cheers,
    Chris.



  • I think if you have a QML like:

    @
    Item{
    id:"myRoot"
    Button{
    id:"button"
    }
    }
    @

    You could find the child whit id:"button" and use it. I would say something like:
    @
    QObject* myButton = viewer.findChild<QObject*>("button");
    @

    And connect it's signals and slots from "myButton" object.



  • No, not with the identifier attribute. You need to set the objectName property instead.

    eg:
    @
    Item {
    id: anId
    objectName: aName
    }
    @

    you'd see:

    @
    QObject byId = viewer.findChild<QObject>("anId"); // NULL
    QObject byName = viewer.findChild<QObject>("aName"); // not null.
    @

    Cheers,
    Chris.



  • [quote author="chrisadams" date="1378340124"]No, not with the identifier attribute. You need to set the objectName property instead.

    eg:
    @
    Item {
    id: anId
    objectName: aName
    }
    @

    you'd see:

    @
    QObject byId = viewer.findChild<QObject>("anId"); // NULL
    QObject byName = viewer.findChild<QObject>("aName"); // not null.
    @

    Cheers,
    Chris.[/quote]

    Ohh yes you are right you need to use the ojectName attribute, my bad.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.