Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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]



  • 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]



  • yes, constructor is missing in your code.



  • [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?


Log in to reply