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. Connect QML signal to CPP slot, or method [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Connect QML signal to CPP slot, or method [SOLVED]

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.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.
  • N Offline
    N Offline
    Neon_Burg
    wrote on last edited by
    #1

    I want to establish a connection between CPP class methods and QML file, but i always get a strange errors, that i cant understand... Help me somebody !

    main.cpp

    @#include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QtDeclarative/QDeclarativeView>
    #include <QtDeclarative/QDeclarativeContext>
    #include <QGraphicsObject>
    #include <QDebug>

    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[])
    {
    QApplication app(argc, argv);

    //QQmlApplicationEngine engine;
    //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
    QDeclarativeView view(QUrl(QStringLiteral("main.qml")));
    QObject *item = view.rootObject();
    
    MyClass myClass;
    QObject::connect(item, SIGNAL(qmlSignal(QString)),
                          &myClass, SLOT(cppSlot(QString)));
    view.show();
    
    return app.exec&#40;&#41;;
    

    }@

    main.qml

    @import QtQuick 2.3
    import QtQuick.Controls 1.2

    ApplicationWindow {
    id: item
    visible: true
    width: 640
    height: 480
    color: "#ffffff"
    title: qsTr("Hello World")
    signal qmlSignal(string msg)

    Button {
        id: button1
        x: 27
        y: 37
        width: 97
        height: 61
        text: qsTr("Button")
        onClicked: item.cppSlot("Hello C++ from QML !!!")
    }
    

    }
    @

    Trying to run this app, i get an errors like this:

    C:\Qt\PROJECTS\QML_with_C\main.cpp:24: error: undefined reference to `_imp___ZN16QDeclarativeViewC1ERK4QUrlP7QWidget'

    and this

    C:\Qt\PROJECTS\QML_with_C\main.cpp:8: error: undefined reference to `vtable for MyClass'

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rolias
      wrote on last edited by
      #2

      It looks like you're just experimenting and learning but I thought I would point out in case you don't know that: QML is automatically aware of Qt C++ slots so you could eliminate the QML signal, the QObject::connect statement (not to mention the curly braces in your onclicked handler). Your QML would just call the slot directly:

      @onClicked: myObj.cppSlot("Your message here.")@

      Check out my third course in the trilogy on Qt
      "Integrating Qt Quick with C++"
      http://bit.ly/qtquickcpp
      published by Pluralsight

      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