Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to control onClicked in qml with C ++ code ?

How to control onClicked in qml with C ++ code ?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
7 Posts 2 Posters 2.6k Views
  • 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.
  • L Offline
    L Offline
    LeeK
    wrote on 19 Feb 2019, 03:05 last edited by
    #1

    i want to control onClicked in qml with c++ code, without touch the display.
    but, i don't know how.
    please tell me....

    ex)
    int main() {
    ...
    callclick();
    ...
    }

    if i do this, call onClick in QML code.

    J 1 Reply Last reply 19 Feb 2019, 05:59
    0
    • L LeeK
      19 Feb 2019, 03:05

      i want to control onClicked in qml with c++ code, without touch the display.
      but, i don't know how.
      please tell me....

      ex)
      int main() {
      ...
      callclick();
      ...
      }

      if i do this, call onClick in QML code.

      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 19 Feb 2019, 05:59 last edited by J.Hilk
      #2

      hi @LeeK and welcome

      first of, you do not provide enough information, for any good help.

      Let me ask this, do you know how to send signals from a c++ class to your qml file?
      If not, you may want to read through this first.
      https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • L Offline
        L Offline
        LeeK
        wrote on 22 Feb 2019, 05:48 last edited by
        #3

        hi @J-Hilk .
        First, thanks for the answers.
        I will provide additional information.

        my sample qml code :

        Rectangle {
             id: rectsampleid
             height: 500
             width: 500
             visible: true
        
            function myQmlFunction(msg) {
                console.log("Got message:", msg)
                return "some return value"
            }
        
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    Qt.quit();
                }
            }
        
            Text {
                text: qsTr("Hello World")
                anchors.centerIn: parent
            }
        }
        

        and my sample main.cpp :

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlComponent>
        #include <QDebug>
        #define QARG(object,returnedValue, msg) QMetaObject::invokeMethod(object, "myQmlFunction", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, msg))
        
        int main(int argc, char *argv[])
        {
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
            //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            QObject *mainPage = component.create();
            QObject* item = mainPage->findChild<QObject *>("buttonTest");
        
            QVariant returnedValue;
            QVariant msg = "Hello from C++";
            QARG(mainPage, returnedValue, msg);
            qDebug() << "QML function returned:" << returnedValue.toString();
        
            return app.exec();
        }
        

        The code is roughly the sample code.
        i want to execute the onClicked in the MouseArea in the QML without touch to display.

        I want to do any method call that gave the signal.
        Otherwise, i want to control the Qt sublayer to achieve the desired result.
        but, i don't know how.....
        And i have already seen the tutorial website you provided, but I couldn't get the information I needed.

        I keep searching and studying, but I can't think of any way.
        Please help me...

        J 1 Reply Last reply 22 Feb 2019, 06:02
        0
        • L LeeK
          22 Feb 2019, 05:48

          hi @J-Hilk .
          First, thanks for the answers.
          I will provide additional information.

          my sample qml code :

          Rectangle {
               id: rectsampleid
               height: 500
               width: 500
               visible: true
          
              function myQmlFunction(msg) {
                  console.log("Got message:", msg)
                  return "some return value"
              }
          
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      Qt.quit();
                  }
              }
          
              Text {
                  text: qsTr("Hello World")
                  anchors.centerIn: parent
              }
          }
          

          and my sample main.cpp :

          #include <QGuiApplication>
          #include <QQmlApplicationEngine>
          #include <QQmlComponent>
          #include <QDebug>
          #define QARG(object,returnedValue, msg) QMetaObject::invokeMethod(object, "myQmlFunction", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, msg))
          
          int main(int argc, char *argv[])
          {
              QGuiApplication app(argc, argv);
          
              QQmlApplicationEngine engine;
              QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
              //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              QObject *mainPage = component.create();
              QObject* item = mainPage->findChild<QObject *>("buttonTest");
          
              QVariant returnedValue;
              QVariant msg = "Hello from C++";
              QARG(mainPage, returnedValue, msg);
              qDebug() << "QML function returned:" << returnedValue.toString();
          
              return app.exec();
          }
          

          The code is roughly the sample code.
          i want to execute the onClicked in the MouseArea in the QML without touch to display.

          I want to do any method call that gave the signal.
          Otherwise, i want to control the Qt sublayer to achieve the desired result.
          but, i don't know how.....
          And i have already seen the tutorial website you provided, but I couldn't get the information I needed.

          I keep searching and studying, but I can't think of any way.
          Please help me...

          J Online
          J Online
          J.Hilk
          Moderators
          wrote on 22 Feb 2019, 06:02 last edited by
          #4

          @LeeK said in How to control onClicked in qml with C ++ code ?:

          hi @J-Hilk .
          First, thanks for the answers.
          I will provide additional information.

          thanks

          And i have already seen the tutorial website you provided, but I couldn't get the information I needed.

          I doubt that, there is exactly an example of what you need:
          https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods

          and here's a working program for you:

          //main.cpp
          #include <QApplication>
          #include <QQmlApplicationEngine>
          
          #include <QDebug>
          
          int main(int argc, char *argv[])
          {
              QApplication app(argc, argv);
          
              QQmlApplicationEngine engine;
          
              engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              if (engine.rootObjects().isEmpty())
                  return -1;
          
              QObject *rootElement = engine.rootObjects().first();
              QVariant returnedValue;
              QVariant msg = "Hello from C++";
              QMetaObject::invokeMethod(rootElement, "myQmlFunction",
                      Q_RETURN_ARG(QVariant, returnedValue),
                      Q_ARG(QVariant, msg));
          
              qDebug() << "QML function returned:" << returnedValue.toString();
          
              return app.exec();
          }
          
          //main.qml
          import QtQuick 2.11
          import QtQuick.Window 2.2
          
          Window
          {
              id:rootWindow
              width: 400
              height: 250
          
              visible:  true
          
              function myQmlFunction(msg) {
                      console.log("Got message:", msg)
                      return "some return value"
                  }
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            LeeK
            wrote on 22 Feb 2019, 06:32 last edited by
            #5

            okay, i know.
            The tutorial you provided is what I saw in the past, and I wrote the sample code through it.
            However, what I want to ask is whether i can control "onClicked" in qml with C ++ code based on that sample code, not that the sample code is wrong.

            J 1 Reply Last reply 22 Feb 2019, 06:38
            0
            • L LeeK
              22 Feb 2019, 06:32

              okay, i know.
              The tutorial you provided is what I saw in the past, and I wrote the sample code through it.
              However, what I want to ask is whether i can control "onClicked" in qml with C ++ code based on that sample code, not that the sample code is wrong.

              J Online
              J Online
              J.Hilk
              Moderators
              wrote on 22 Feb 2019, 06:38 last edited by
              #6

              @LeeK ah, I got you now ;-)

              The answer is yes and no

              you can't directly access the onClicked function, but you could create a standalone function that is called/executed by onClicked and can also be called from c++

              Or you could potentially emit the clicked signal manually, I'm unsure about that as I haven' tried that one yet.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                LeeK
                wrote on 22 Feb 2019, 06:47 last edited by LeeK
                #7

                @J-Hilk okay.
                "you can't directly access the onClicked function."
                maybe, this is answer for my questions.
                thanks for the replies.

                1 Reply Last reply
                0

                1/7

                19 Feb 2019, 03:05

                • Login

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