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. Interacting with QML Objects from C++
Forum Updated to NodeBB v4.3 + New Features

Interacting with QML Objects from C++

Scheduled Pinned Locked Moved QML and Qt Quick
qml qt signal p
22 Posts 2 Posters 7.5k Views 2 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.
  • K Khachatur

    @p3c0

    Yes, I mean that there are two instances of main.qml.

    Doing this you will get the new instance of main.qml:

    QQmlApplicationEngine anEngine;
    anEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    QObject *aRoot = anEngine.rootObjects()[0];
    

    I didn't get what does it mean "Which are they"?

    p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by
    #13

    @Khachatur How did you check whether they are 2 separate instance ?

    157

    K 1 Reply Last reply
    0
    • p3c0P p3c0

      @Khachatur How did you check whether they are 2 separate instance ?

      K Offline
      K Offline
      Khachatur
      wrote on last edited by
      #14

      @p3c0

      I don't know the way how to check directly whether they are 2 separate instances, but I was relying on logic.

      In QML slots onColorChanged and onVisibleChanged of test_rect I am printing the 'color' and 'visible' values.

              onColorChanged: {
                  console.log(color)
              }
      
              onVisibleChanged: {
                  console.log(visible)
              }
      

      When I am changing property of QML objects from C++ code:

                aRect->setProperty("color", "green");
                aRect->setVisible(false);
      

      In output console I get the correct values of color and visibility (green and false respectively), but rectangle still remains red and visible.

      Therefore I make decision that I am working with another instance of main.qml and I was right.

      p3c0P 1 Reply Last reply
      0
      • K Khachatur

        @p3c0

        I don't know the way how to check directly whether they are 2 separate instances, but I was relying on logic.

        In QML slots onColorChanged and onVisibleChanged of test_rect I am printing the 'color' and 'visible' values.

                onColorChanged: {
                    console.log(color)
                }
        
                onVisibleChanged: {
                    console.log(visible)
                }
        

        When I am changing property of QML objects from C++ code:

                  aRect->setProperty("color", "green");
                  aRect->setVisible(false);
        

        In output console I get the correct values of color and visibility (green and false respectively), but rectangle still remains red and visible.

        Therefore I make decision that I am working with another instance of main.qml and I was right.

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by p3c0
        #15

        @Khachatur No they are exactly the same objects.
        Consider the following example:
        main.qml

        import QtQuick 2.4
        import QtQuick.Window 2.2
        
        Window {
            visible: true
            width: 150
            height: 150
        
            Rectangle {
                anchors.fill: parent
            }
        
            Component.onCompleted: console.log("From QML: ",this) // prints the root object
        }
        

        your main.cpp

        QQmlApplicationEngine anEngine;
        anEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        
        QObject *aRoot = anEngine.rootObjects()[0];
        qDebug() << "From C++: " << aRoot;
        

        After running if you look at the outputs you can see the same address on both sides which indicates it is the same object.
        I think there is some other problem in your code.
        Can you post or upload an updated complete minimal working example which will show the problem ?

        157

        K 2 Replies Last reply
        0
        • p3c0P p3c0

          @Khachatur No they are exactly the same objects.
          Consider the following example:
          main.qml

          import QtQuick 2.4
          import QtQuick.Window 2.2
          
          Window {
              visible: true
              width: 150
              height: 150
          
              Rectangle {
                  anchors.fill: parent
              }
          
              Component.onCompleted: console.log("From QML: ",this) // prints the root object
          }
          

          your main.cpp

          QQmlApplicationEngine anEngine;
          anEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          
          QObject *aRoot = anEngine.rootObjects()[0];
          qDebug() << "From C++: " << aRoot;
          

          After running if you look at the outputs you can see the same address on both sides which indicates it is the same object.
          I think there is some other problem in your code.
          Can you post or upload an updated complete minimal working example which will show the problem ?

          K Offline
          K Offline
          Khachatur
          wrote on last edited by
          #16

          @p3c0

          I have checked.

          This code I am using at the first time in file main.cpp when I am launching the application

          QQmlApplicationEngine anEngine;
          anEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          
          QObject *aRoot = anEngine.rootObjects()[0];
          qDebug() << "From C++: " << aRoot;
          

          Then I am using the same code in my OperationCreatePoint.cpp class and I get the different address of aRoot.

          The output is:
          From main.cpp: ApplicationWindow_QMLTYPE_57_QML_74(0x4711600)
          From OperationCreatePoint.cpp: ApplicationWindow_QMLTYPE_143_QML_160(0x8478fc0)

          p3c0P 1 Reply Last reply
          0
          • K Khachatur

            @p3c0

            I have checked.

            This code I am using at the first time in file main.cpp when I am launching the application

            QQmlApplicationEngine anEngine;
            anEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            
            QObject *aRoot = anEngine.rootObjects()[0];
            qDebug() << "From C++: " << aRoot;
            

            Then I am using the same code in my OperationCreatePoint.cpp class and I get the different address of aRoot.

            The output is:
            From main.cpp: ApplicationWindow_QMLTYPE_57_QML_74(0x4711600)
            From OperationCreatePoint.cpp: ApplicationWindow_QMLTYPE_143_QML_160(0x8478fc0)

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by p3c0
            #17

            @Khachatur Some confusion. Do you mean you load main.qml twice ? One is main.cpp and another in OperationCreatePoint.cpp ?
            Atleast from the output it seems.

            157

            1 Reply Last reply
            0
            • p3c0P p3c0

              @Khachatur No they are exactly the same objects.
              Consider the following example:
              main.qml

              import QtQuick 2.4
              import QtQuick.Window 2.2
              
              Window {
                  visible: true
                  width: 150
                  height: 150
              
                  Rectangle {
                      anchors.fill: parent
                  }
              
                  Component.onCompleted: console.log("From QML: ",this) // prints the root object
              }
              

              your main.cpp

              QQmlApplicationEngine anEngine;
              anEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              
              QObject *aRoot = anEngine.rootObjects()[0];
              qDebug() << "From C++: " << aRoot;
              

              After running if you look at the outputs you can see the same address on both sides which indicates it is the same object.
              I think there is some other problem in your code.
              Can you post or upload an updated complete minimal working example which will show the problem ?

              K Offline
              K Offline
              Khachatur
              wrote on last edited by
              #18

              @p3c0

              English is not my native language, may be you misunderstood me... Anyway thank you for your response and sorry for misunderstood

              p3c0P 1 Reply Last reply
              0
              • K Khachatur

                @p3c0

                English is not my native language, may be you misunderstood me... Anyway thank you for your response and sorry for misunderstood

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #19

                @Khachatur That's fine :)
                If you still have the problem you can post the updated complete minimal working example so that it would be more helpful in finding the problem.

                157

                K 1 Reply Last reply
                0
                • p3c0P p3c0

                  @Khachatur That's fine :)
                  If you still have the problem you can post the updated complete minimal working example so that it would be more helpful in finding the problem.

                  K Offline
                  K Offline
                  Khachatur
                  wrote on last edited by
                  #20

                  @p3c0

                  Yes, I meant I was loading main.qml twice in main.cpp and OperationCreatePoint.cpp. Currently, I have not any problem, Thanks :)

                  I just have another question about QML FileDialog component, but I guess... should I create new thread?

                  I am using the FileDialog to load and save files. When I am saving file I want to pass to my FileDialog the default file name, but unfortunately FileDialog have not such property, only fileUrl property which is ReadOnly. I don't want to implement my own file dialog component...

                  p3c0P 1 Reply Last reply
                  0
                  • K Khachatur

                    @p3c0

                    Yes, I meant I was loading main.qml twice in main.cpp and OperationCreatePoint.cpp. Currently, I have not any problem, Thanks :)

                    I just have another question about QML FileDialog component, but I guess... should I create new thread?

                    I am using the FileDialog to load and save files. When I am saving file I want to pass to my FileDialog the default file name, but unfortunately FileDialog have not such property, only fileUrl property which is ReadOnly. I don't want to implement my own file dialog component...

                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #21

                    @Khachatur AFAIK it is still not implemented. Taking this into consideration some one has already implemented their own. See here.

                    157

                    K 1 Reply Last reply
                    0
                    • p3c0P p3c0

                      @Khachatur AFAIK it is still not implemented. Taking this into consideration some one has already implemented their own. See here.

                      K Offline
                      K Offline
                      Khachatur
                      wrote on last edited by
                      #22

                      @p3c0

                      That is exactly what I need. Thank you :)

                      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