Please nominate your Qt Champions for 2021!

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.

    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]#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{

    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->setProperty("theurl", "");
    Blubb blubb;
    QObject::connect(item, SIGNAL(stoppedSignal(QString)), &blubb, SLOT(hasStopped(QString)));
    return app.exec();


  • 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
    explicit Blubb(QObject *parent = 0);


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

    #endif // BLUBB_H

  • yes, constructor is missing in your code.

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

    I had at some point something like this:

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

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

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

Log in to reply