Problem with C++ signal to QML

  • Hi,

    I know there is plenty of documentation on this but since I'm a C++ beginner, some of the constructions don't make much sense to me. Also almost everybody is searching for the opposite direction - QML signal to C++. There is not much written about propagating signals from C++ to QML.

    Anyway I found his "guide": and I based my code on it. Here is what I created:


    @#ifndef MEDIASCAN_H
    #define MEDIASCAN_H
    #include "filesearch.h"
    #include "mediatag.h"
    #include <QDebug>

    #include <QObject>

    class MediaScan : public QObject

    explicit MediaScan(QObject *parent = 0);
    Q_INVOKABLE void scan(const QStringList &directories);

    void fileCountUpdate(int &fileCount);
    void filesFound();
    void progress(int &progress);
    void filesScanned(QList<QStringList> &mediaTags);

    public slots:
    void onFileCountUpdate(const int &progress);
    void onProgress(const int &progress);
    void onFilesFound(const QStringList &fileList);

    #endif // MEDIASCAN_H


    @#include <QtGui/QGuiApplication>
    #include <QQmlContext>
    #include "qtquick2applicationviewer.h"
    #include "mediascan.h"

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

    QtQuick2ApplicationViewer viewer;
    QQmlContext* ctxt = viewer.rootContext();
    MediaScan* mediascan = new MediaScan();
    ctxt->setContextProperty("mediascan", mediascan);
    QQuickItem *rootObject = viewer.rootObject();
    QObject::connect(&mediascan, SIGNAL(filesFound()), rootObject, SLOT(filesFound()));
    return app.exec&#40;&#41;;


    What the code does is that it gets some paths from QML UI and runs the scan function. Then I create a new thread which searches for media files in the directories and returns back some information (signals) about the progress. I think that what is inside mediascan.cpp is not important.

    What happens when I try to compile the code is that I get following error:
    @C:\Qt\projects\Muzika\main.cpp:18: note: candidate expects 3 arguments, 4 provided
    QObject::connect(&mediascan, SIGNAL(filesFound()), rootObject, SLOT(filesFound()));

    This absolutely doesn't make sense to me. In all references I found the connect function needs 4 arguments. What is wrong in my code? And is this the right way to connect signals with QML UI?

  • Moderators


    QObject::connect(&mediascan, SIGNAL(filesFound()), rootObject, SLOT(filesFound()));
    NO need to pass the address (&mediascan) here. It expects a pointer.
    So just

    QObject::connect(mediascan, SIGNAL(filesFound()), rootObject, SLOT(filesFound()));
    should work

  • Thanks for the tip. After some more tweaking I finally made it work.

Log in to reply

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