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":http://developer.nokia.com/Community/Wiki/Connecting_Qt_signal_to_QML_function and I based my code on it. Here is what I created:
mediascan.h
@#ifndef MEDIASCAN_H
#define MEDIASCAN_H
#include "filesearch.h"
#include "mediatag.h"
#include <QDebug>#include <QObject>
class MediaScan : public QObject
{
Q_OBJECTpublic:
explicit MediaScan(QObject *parent = 0);
Q_INVOKABLE void scan(const QStringList &directories);signals:
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
@main.cpp
@#include <QtGui/QGuiApplication>
#include <QQmlContext>
#include "qtquick2applicationviewer.h"
#include "mediascan.h"int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/Muzika/main.qml")); viewer.showExpanded(); 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();
}
@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?
-
Hi,
@
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