Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    From C++ consume QML signal (Qt5/Qml2)

    General and Desktop
    2
    4
    2112
    Loading More Posts
    • 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.
    • A
      archdove last edited by

      Hello

      I'm just at the beginning of learning C++/Qt and i stumble over some basics.

      My Qml file exposes a signal stoppedSignal(string msg). I would like to consume this signal from within c++, but i get strange errors instead.

      Errors:
      in function 'Blubb:Blubb()':
      undefined reference to 'vtable for Blubb'
      in function 'Blubb::~Blubb()':
      undefined reference to 'vtable for Blubb'

      I've tried several ways and this is the most current, which is a rewrite of a tutorial actually. I would like to use the "new way" of connecting signals, but there seems to be no way of getting an actual function pointer to a qml signal.

      So whats wrong here?

      [code]import QtQuick 2.0
      import QtQuick.Window 2.0
      import QtMultimedia 5.0

      Window {
      id: mainWindow
      width: 100
      height: 62

      property string thefile: "/home/sfuser/Downloads/video1.mp4"
      signal stoppedSignal(string msg)

      Rectangle {
      id: rec
      anchors.fill: parent

      Video {
        id: videoPlayer
        anchors.fill: parent
        source: mainWindow.thefile
        focus:  true
        autoPlay: true
        onStopped: mainWindow.stoppedSignal("videoX");
      }
      

      }
      }[/code]
      [code]#include <QtGui/QGuiApplication>
      #include <QtQuick/QQuickView>
      #include <QQmlProperty>
      #include <QQmlEngine>
      #include <QQmlComponent>
      #include <QQuickItem>
      #include <QDebug>
      #include <QObject>
      #include <QString>
      #include "qtquick2applicationviewer.h"

      class Blubb : public QObject{
      Q_OBJECT

      public slots:
      void hasStopped(const QString &msg){
      qDebug() << "Video has stopped! " << msg;
      }
      };

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

      QQmlEngine engine;
      QQmlComponent component(&engine, QUrl("qml/interaction2/Video.qml"));
      QObject *object = component.create();
      
      QQuickWindow *item = qobject_cast<QQuickWindow*>(object);
      item->setHeight(300);
      item->setWidth(600);
      //item->setProperty("theurl", "http://www.youtube.de");
      
      Blubb blubb;
      QObject::connect(item, SIGNAL(stoppedSignal(QString)), &blubb, SLOT(hasStopped(QString)));
      
      return app.exec();
      

      }[/code]

      1 Reply Last reply Reply Quote 0
      • A
        archdove last edited by

        I recreated the Blubb class with QtCreator. The result was this code which looks nearly the same as mine. But not completly.

        Can anyone explain to me what happened?

        [code]#ifndef BLUBB_H
        #define BLUBB_H

        #include <QObject>
        #include <QDebug>

        class Blubb : public QObject
        {
        Q_OBJECT
        public:
        explicit Blubb(QObject *parent = 0);

        signals:

        public slots:
        void hasStopped() {
        qDebug() << "Video has stopped!";
        }
        };

        #endif // BLUBB_H
        [/code]

        1 Reply Last reply Reply Quote 0
        • S
          Serenity last edited by

          yes, constructor is missing in your code.

          1 Reply Last reply Reply Quote 0
          • A
            archdove last edited by

            [quote author="Serenity" date="1365576471"]yes, constructor is missing in your code.
            [/quote]

            I had at some point something like this:
            [code]
            public:
            Blubb();
            ~Blubb():
            [/code]

            This did not help. The Parameter the constructor is expecting is mandatory for QObjects i presume, which would explain the error.

            Is the syntax
            [Code]
            QObject *parent = 0 [/code]

            A default initialization of the parameter in case nothing is provided otherwise?

            1 Reply Last reply Reply Quote 0
            • First post
              Last post