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 signal to cpp

Qml signal to cpp

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 1.9k 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.
  • G Offline
    G Offline
    gpio
    wrote on last edited by
    #1

    Hello,

    there are a lot of question with similar content.

    I use the qt stack qml and i want to send a signal from a qml to the cpp.
    In this documentation they use "view" but since i have a engine this wont work
    http://doc.qt.io/archives/qt-4.8/qtbinding.html#defining-new-qml-elements

    rootitem->dumpObjectInfo();
    in my cpp i can see the application window but to find the "child" of the main is not realy easy and a think it is also the wrong way to do this like i tryed.

    i mean the compiler says that the qmlsignal function is not supported
    onClicked: switch1.qmlSignal("on")

    Switch {
    id: switch1
    x: 41
    y: 293
    width: 200
    height: 40
    text: qsTr("Pumpe")
    font.pointSize: 16

        signal qmlSignal(string msg)
        
        onClicked: switch1.qmlSignal("on")
    }
    

    so i hope you can help me!
    Thanks.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gpio
      wrote on last edited by
      #7

      Hello,

      thanks for your reply.
      i found a simple way to do this.
      First take this code into the main

      #include <QQmlContext>

      ...
      QGuiApplication app(argc, argv);
      QQmlApplicationEngine engine;

      communicationbmlogic contextPropertyClass;
      QQmlContext* context = engine.rootContext();
      context->setContextProperty("contextPropertyClass", &contextPropertyClass);
      

      ...

      now just go to the ui file and make a connection with "add binding connection" in the left toolbar.
      In my case i took the "onToggeled" option end in the "Action" i put this code
      contextPropertyClass.on_buttonPumpOnOff_clicked(switch1.checked)

      now there are seperate connections.

      Connections {
          target: switch1
          onToggled: contextPropertyClass.on_buttonPumpOnOff_clicked(switch1.checked)
      }
      

      now every time i hit the button the class get "true" ore "false"
      perfect!

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gpio
        wrote on last edited by
        #2

        maybe on step depper. in case i take the object:

        QList<QObject*> item = engine.rootObjects();
        qDebug() << item;
        

        then i just see the

        (QQuickApplicationWindow(0x2071ef5f300))

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gpio
          wrote on last edited by
          #3

          i think i achieved the first step.

          int main(int argc, char *argv[])
          {
          ...

          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
          
          communicationbmlogic comob;
          QQmlContext* context = engine.rootContext();
          context->setContextProperty("comob", &comob);
          

          but now i have a different problem.
          the switch is located in a different page (ManaulForm.ui.qml)
          But since this is a ui i should not write "user code" into it.
          So how can i create a connection between die ManualForm.ui.qml and the main.qml ore a ManualForm.qml?!?

          I need the Signal from the button in a qml which can communicate with the cpp

          1 Reply Last reply
          0
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #4

            @gpio this is working code (Qt 5.9.0), mostly adapted from this documentation. Note I'm using Switch.checked to better reflect the status of the widget, selected (true) or not (false) to the C++ side.

            main.qml

            import QtQuick 2.6
            import QtQuick.Window 2.2
            import QtQuick.Controls 1.4
            
            Window {
                id: window
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")
                signal qmlSignal(string msg)
            
                Switch {
                    id: switch1
                    x: 41
                    y: 293
                    width: 200
                    height: 40
            
                    onClicked: window.qmlSignal(switch1.checked)
                }
            }
            

            myclass.h

            #ifndef MYCLASS_H
            #define MYCLASS_H
            #include <QObject>
            #include <QDebug>
            
            class MyClass : public QObject
            {
                Q_OBJECT
            
            public slots:
                void cppSlot(const QString &msg) {
                    qDebug() << "Called the C++ slot with message: " << msg;
                }
            };
            #endif // MYCLASS_H
            

            main.cpp

            #include <QGuiApplication>
            #include <QQmlApplicationEngine>
            #include <QList>
            #include <QQuickWindow>
            #include "myclass.h"
            
            int main(int argc, char *argv[])
            {
                QGuiApplication app(argc, argv);
            
                QQmlApplicationEngine engine;
                engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                if (engine.rootObjects().isEmpty())
                    return -1;
            
                MyClass myClass;
                QQuickWindow* window = (QQuickWindow*) engine.rootObjects().first();
                QObject::connect(window, SIGNAL(qmlSignal(QString)), &myClass, SLOT(cppSlot(QString)));
            
                return app.exec();
            }
            

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #5

              Don't do that.

              Expose your c++ object to qml (as a context property or with qmlRegisterXXX) and then call a slot or a Q_INVOKABLE method of this object from QML.

              onClicked: cppObject.cppSlot("on")

              Not reaching into your UI layer from c++ will allow to keep a clean codebase and easier refactoring.
              Some more in-depth reasoning about this : https://www.youtube.com/watch?v=vzs5VPTf4QQ&feature=youtu.be&t=23m20s

              1 Reply Last reply
              3
              • Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #6

                @GrecKo thank you for pointing this out. I see the problems of going from C++ to QML as stated in the video (i.e. you change button into slider), great talk by the way.
                So my take-away is: always go from QML to C++ (I think C++ code as a "library" used by QML code).

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gpio
                  wrote on last edited by
                  #7

                  Hello,

                  thanks for your reply.
                  i found a simple way to do this.
                  First take this code into the main

                  #include <QQmlContext>

                  ...
                  QGuiApplication app(argc, argv);
                  QQmlApplicationEngine engine;

                  communicationbmlogic contextPropertyClass;
                  QQmlContext* context = engine.rootContext();
                  context->setContextProperty("contextPropertyClass", &contextPropertyClass);
                  

                  ...

                  now just go to the ui file and make a connection with "add binding connection" in the left toolbar.
                  In my case i took the "onToggeled" option end in the "Action" i put this code
                  contextPropertyClass.on_buttonPumpOnOff_clicked(switch1.checked)

                  now there are seperate connections.

                  Connections {
                      target: switch1
                      onToggled: contextPropertyClass.on_buttonPumpOnOff_clicked(switch1.checked)
                  }
                  

                  now every time i hit the button the class get "true" ore "false"
                  perfect!

                  1 Reply Last reply
                  0
                  • Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #8

                    @gpio please if your issue is solved don't forget to mark your post as such. Thanks.

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    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