Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Problem with C++ signal to QML

Problem with C++ signal to QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.3k Views
  • 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.
  • J Offline
    J Offline
    jech
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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&#40;QStringLiteral("qml/Muzika/main.qml"&#41;&#41;;
    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&#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?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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

      157

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jech
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved