Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML type representation in C++ code
Forum Updated to NodeBB v4.3 + New Features

QML type representation in C++ code

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    osmial
    wrote on last edited by osmial
    #1

    Hello,

    I'm trying to find out if QML types have their representation in C++ code.
    Here we have whole qml types list: http://doc.qt.io/qt-5/qmltypes.html 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()));
        }
    
        view.show();
        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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        O Offline
        O Offline
        osmial
        wrote on last edited by
        #3

        @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++ :)

        1 Reply Last reply
        0
        • johngodJ Offline
          johngodJ Offline
          johngod
          wrote on last edited by
          #4

          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.

          O 1 Reply Last reply
          0
          • johngodJ johngod

            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.

            O Offline
            O Offline
            osmial
            wrote on last edited by
            #5

            @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: https://forum.qt.io/topic/72407/connect-qml-signal-with-c-slot/7 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 :)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              @Wieland's approach is indeed a good solution.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved