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. QThread signal to QML
Forum Updated to NodeBB v4.3 + New Features

QThread signal to QML

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 2.5k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    foska
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • GianlucaG Offline
      GianlucaG Offline
      Gianluca
      wrote on last edited by
      #2

      Remove the quotes around the name of the target:

      @
      Connections {
      target: myCard
      onMyValueHasChanged { }
      }
      @

      1 Reply Last reply
      0
      • F Offline
        F Offline
        foska
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          melghawi
          wrote on last edited by
          #4

          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).

          1 Reply Last reply
          0
          • F Offline
            F Offline
            foska
            wrote on last edited by
            #5

            [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!

            1 Reply Last reply
            -1
            • M Offline
              M Offline
              melghawi
              wrote on last edited by
              #6

              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.

              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