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. Beginner Question: Connecting to QML Signals
Forum Updated to NodeBB v4.3 + New Features

Beginner Question: Connecting to QML Signals

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 5 Posters 2.5k 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.
  • F Offline
    F Offline
    fornax
    wrote on last edited by
    #1

    Hey,

    i try to connect cpp with QML and had a build Error that i can't resolve.

    I copyed the code based on website (doc-snapshot.qt-project.org/5.0/qtqml/qtqml-cppintegration-interactqmlfromcpp.html)
    Subtopic "Connecting to QML Signals"

    @// MyItem.qml
    import QtQuick 2.0

    Item {
    id: item
    width: 100; height: 100

    signal qmlSignal(string msg)
    
    MouseArea {
        anchors.fill: parent
        onClicked: item.qmlSignal("Hello from QML")
    }
    

    }@

    @class MyClass : public QObject
    {
    Q_OBJECT
    public slots:
    void cppSlot(const QString &msg) {
    qDebug() << "Called the C++ slot with message:" << msg;
    }
    };

    int main(int argc, char *argv[]) {
    QGuiApplication app(argc, argv);

    QQuickView view(QUrl::fromLocalFile&#40;"MyItem.qml"&#41;);
    QObject *item = view.rootObject();
    
    MyClass myClass;
    QObject::connect(item, SIGNAL(qmlSignal(QString)),
                     &myClass, SLOT(cppSlot(QString)));
    
    view.show();
    return app.exec();
    

    }@

    If i try to run it i get the error:
    Error:C2440: 'Initialition': 'QQuickView' can not in 'QObject *' convert ***(i translate the error from german to english)

    Have any one a good tip to solve this ?

    I try some variants with dynamic cast i found on stack overflow but without any success :(

    Thank you for your help !

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Signal does not belong to rootObject, but to "item" child.

      Add this to QML:
      @
      id: item
      objectName: "item"
      @

      And this to c++:
      @
      // to get the object that defines the signal:
      QObject *item = view.rootObject().findChild<QObject *>("item");
      @

      (Z(:^

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fahmy
        wrote on last edited by
        #3

        This is because view.rootObject() return QQuickItem * insted of QObject *. Unless you using sierdzio's suggestion.

        Stay hungry, stay foolish.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          Nantunest
          wrote on last edited by
          #4

          Hello,

          I am returning to this question to ask if the solution presented is the best way to connect qml signals to C++ slots.

          Say, If in my code I load the main qml window, I will have to find each qml instance by name and connect to a slot?

          Thank You

          1 Reply Last reply
          0
          • S Offline
            S Offline
            samdbgt
            wrote on last edited by
            #5

            I have been trying to use the Qml-grid view in my code. I am on a mission to couple it with my C++ code which I have been in vain so far. I think my problem occurs when I try to generate the signal from the qml rectangle. I will definitely update you with the results once I have done with it.

            "super-beta-prostate.com":http://www.super-beta-prostate.com

            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