[SOLVED] Signals and slots in QML (Qt5, QtQuick 2)



  • http://qt-project.org/doc/qt-4.8/qtbinding.html#receiving-signals

    I have been looking at the above example and searching for more recent examples but I just cannot get my code to work. Can anyone point me in the direction of an example or see what is wrong with the code below? As I say, I am using Qt5

    @
    import QtQuick 2.0

    Rectangle
    {
    id: guide
    width: 800
    height: 600
    signal qmlSignal(string msg)

    property double scaleFactor: 1.0
    property string iconUrl: "Default.png"
    MouseArea
    {
         anchors.fill: parent
         onClicked: guide.qmlSignal("Hello from QML")
    }
    

    }
    @
    main.cpp
    @
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QQmlContext>
    //#include <QDeclarativeContext>
    #include "details.h"

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

    QtQuick2ApplicationViewer  viewer;
    
    viewer.setMainQmlFile&#40;QStringLiteral("qml/GuideNext/main.qml"&#41;&#41;;
    
    
    CDetails *test = new CDetails();
    QObject::connect((QObject*)viewer.rootObject(), SIGNAL(qmlSignal(QString)),test, SLOT(cppSlot(QString)));
    

    @
    details.h
    @class CDetails : public QObject
    {
    Q_OBJECT

    public:
    CDetails();
    ~CDetails(void);

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



  • Hi,

    Hm... The code looks fine and work for me. Can you show error message if it exists? The source code of my test projects below:

    main.qml
    @
    import QtQuick 2.0

    Rectangle
    {
    id: guide
    width: 800
    height: 600

    signal qmlSignal(string msg)
    
    property double scaleFactor: 1.0
    property string iconUrl: "Default.png"
    MouseArea
    {
         anchors.fill: parent
         onClicked: guide.qmlSignal("Hello from QML")
    }
    

    }
    @

    main.cpp
    @
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"

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

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/test/main.qml"&#41;);
    viewer.showExpanded();
    
    QObject::connect((QObject*)viewer.rootObject(), SIGNAL(qmlSignal(QString)), &viewer, SLOT(onMessage(QString)));
    
    return app.exec();
    

    }
    @

    qtquick2applicationviewer.h
    @

    #include <QtQuick/QQuickView>
    #include <QDebug>

    class QtQuick2ApplicationViewer : public QQuickView
    {
    Q_OBJECT

    public:
    explicit QtQuick2ApplicationViewer(QWindow *parent = 0);
    virtual ~QtQuick2ApplicationViewer();

    void setMainQmlFile&#40;const QString &file&#41;;
    void addImportPath(const QString &path);
    
    void showExpanded();
    

    private:
    class QtQuick2ApplicationViewerPrivate *d;

    public slots:
    void onMessage(QString msg)
    {
    qDebug()<<"Message: "<<msg;
    }
    };
    @



  • Yes it is fine which was driving me crazy. Turns out I could not get the debug message because there was a hidden version of the debugger running (so 2 versions of Qt Creator). Ahhhhhh! Give me VS dev environment any day!

    Thanks for your reply.

    Any idea why the application output says my class is not defined in the QML even though it clearly is and is working?? I am just ignoring this at the moment but it is a little odd.



  • Please, use the Qt5 docs if you're using Qt5, instead of the Qt 4.8 docs.

    http://doc-snapshot.qt-project.org/5.0/qtquick/qtquick-applicationdevelopers.html
    http://doc-snapshot.qt-project.org/5.0/qtqml/qtqml-index.html
    http://doc-snapshot.qt-project.org/5.0/qtquick/qtquick-index.html

    What precisely does your application output say? What do you mean by "not defined in the QML"?

    Cheers,
    Chris.



  • There is a runtime error saying the class is not defined even though a) the code works b) when you write the class name in QML it become italic signifying (I would think) that the class is recognised at development time. If you click on the error it takes you to a line in the QML where the class is used.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.