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. [SOLVED] Signals and slots in QML (Qt5, QtQuick 2)
Forum Updated to NodeBB v4.3 + New Features

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

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

    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;
    }
    };@

    1 Reply Last reply
    0
    • shavS Offline
      shavS Offline
      shav
      wrote on last edited by
      #2

      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;
      }
      };
      @

      Mac OS and iOS Developer

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dolphin
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chrisadams
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dolphin
            wrote on last edited by
            #5

            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.

            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