QThread signal to QML



  • Hi all,
    i think this is a dumb question but i can't understand the solution:

    So i have my worker class cardWorker.h
    @
    ...
    class CardWorker : public QObject {
    Q_OBJECT

    public:
    CardWorker();
    ~CardWorker();

    public slots:
    void process();

    signals:
    void finished();
    void error(QString err);
    void myValueHasChanged(int value);

    private:
    // add your variables here
    };
    ...
    @

    The implementation: cardWorker.cpp
    @...
    // --- CONSTRUCTOR ---
    CardWorker::CardWorker() {
    // you could copy data from constructor arguments to internal variables here.
    }

    // --- DESTRUCTOR---
    CardWorker::~CardWorker() {
    // free resources
    }

    // --- PROCESS ---
    // Start processing data.
    void CardWorker::process() {
    // allocate resources using new here
    qDebug("Hello World!");
    emit myValueHasChanged(10);
    emit finished();
    }
    ...@

    In my main.cpp i have;
    @...
    QThread* thread = new QThread;
    CardWorker* worker = new CardWorker();
    worker->moveToThread(thread);

    QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
    QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
    ...@

    Now the problem is: if i have my "main.qml" with a "Text" item how can i "see" the signal "myValueHasChanged()" emitted by the worker?
    I tryed this:

    main.qml
    @...
    Text{
    id:myText

    Connections{
    target: myCard
    onMyValueHasChanged{
    ...do things..
    }
    }
    }
    ...@

    and i added, just after the "thread ->start()" command:
    @
    view.rootContext()->setContextProperty("myCard", worker);
    @

    but this doesn't work. I know this is wrong but i can't understand how to solve this problem.
    All tips are appreciated.

    Thanks in advance!
    Claudio



  • Remove the quotes around the name of the target:

    @
    Connections {
    target: myCard
    onMyValueHasChanged { }
    }
    @



  • Ye sorry, the original code has no quotes, i've just re-written here a small part of the code because i have not the files here at the moment.

    I'll edit the first post.

    Thank you!

    [quote author="Gianluca" date="1418935015"]Remove the quotes around the name of the target:

    @
    Connections {
    target: myCard
    onMyValueHasChanged { }
    }
    @
    [/quote]

    By the way, if it can help, the auto-complete function, hint me the myCard when i write the target into the Connection section, but it doesn't hint the "onMyValueHasChanged".
    When i run the code an error says that it can't find myCard and the function



  • Hi foska,

    Are you getting the following error message by any chance?

    QQmlEngine: Illegal attempt to connect to CardWorker(0x231b9b8) that is in a different thread than the QML engine QQmlEngine(0x2265cc8).



  • [quote author="moeg687" date="1420739719"]Hi foska,

    Are you getting the following error message by any chance?

    QQmlEngine: Illegal attempt to connect to CardWorker(0x231b9b8) that is in a different thread than the QML engine QQmlEngine(0x2265cc8).[/quote]

    Hi!
    I'm sorry but that wasn't my problem. If you want i can send you some files to show you the way i solved my problem because there is too much code to post it here.
    Ciao ciao!



  • No that's fine.

    I do not have this problem. I was trying to help you figure out what was going wrong.

    Nice to hear you've solved it.


Log in to reply
 

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