Important: Please read the Qt Code of Conduct -

QML type representation in C++ code

  • Hello,

    I'm trying to find out if QML types have their representation in C++ code.
    Here we have whole qml types list: and lets say we will take a look on those:

    • Text
    • TextArea: QtQuickControls
    • TextArea: QtQuickControls2
    • TextAreaStyle
    • TextEdit
    • TextField: QtQuickControls
    • TextField: QtQuickControls2
    • TextFieldStyle
    • TextInput
    • TextMetrics
    • TextureImage: Qt3D
    • TextureImage: QtCanvas3D
    • TextureImageFactory

    So we have some text* types, some of them provides signals and slots API.
    It is possible to create connection from C++ code like this way:

    #include <QGuiApplication>
    #include <QQuickView>
    #include <QQuickItem>
    #include "appview.h"
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QQuickView view(QUrl("qrc:/main.qml"));
        QQuickItem* item = qobject_cast<QQuickItem*>(view.rootObject());
        QObject* textField = item->findChild<QObject*>("txt");
        if(textField) {
            textField->setProperty("text", "lorem ipsum");
        QObject* inTxt = item->findChild<QObject*>("inTxt");
        if(inTxt) {
            AppView* appView = new AppView();
            QObject::connect(inTxt, SIGNAL(editingFinished()), appView, SLOT(setTxt()));
        return app.exec();

    Assuming QML part is defined it should work, and it is actually.
    But QObject::connect can be used without SIGNAL/SLOT macros and it is reasonable since using it without those macros enables us to use such features like functors or lambdas. But, to use QObject::connect this way it is needed to provide PointerToMemberFunction to signal and without knowledge about QML object's type representation in C++ we cannot do it.
    So my question is how to find out QML type representation in C++ or if it is impossible/wrong path, what is correct approach to use QObject::connect in non-string signal/slot version.

    Thanks in advance!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Use the new syntax described in the Signals And Slots chapter of Qt's documentation. It provides support for the use cases you described.

  • @SGaist Thanks for your response.
    New syntax is exactly what I'm asking about. I've read some docs about it but I've some concerns about using it.
    As I described, to use this syntax we need to know exact type of sender. But since I'm newbie in QML stuff I have problem with this part - how to find out QML type representation in C++ :)

  • Is it mandatory for you to do the connections in C++ side ? It seems to me it would be easier to do it on qml side.

  • @johngod from architectural point of view my current observation is that better approach is to provide middle-layer like this 'backend' C++ class suggested here: by Wieland than finding QML objects from C++ code and making connections. But since I'm learning QML and wasn't able to find answer to this questions by myself I've decided to ask here :)
    So this is only the matter of curiosity :)

  • Lifetime Qt Champion

    Well, it depends a bit on what you want to achieve within your application.

    @Wieland's approach is indeed a good solution.

Log in to reply