Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with interthread signals
Forum Updated to NodeBB v4.3 + New Features

Problem with interthread signals

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.3k Views 3 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.
  • A Offline
    A Offline
    Andy32
    wrote on last edited by
    #1

    Hi,

    Can anyone please take a look at it. Here is a code snippet.

    I have a Worker object placed into a worker thread. I try to do inter-thread signalling with this object from the main thread (MyWidget)..

    The initialization is done in MyWidget's constructor. There I move the worker object into the worker thread, connect worker's error() signal into MyWidget's clError() slot, connect MyWidget's start() signal into the worker's run() slot, than I start the thread and emit a start() signal.

    The worker's run() slot gets called and from there I emit an error() signal, but the connected MyWidget::clError() slot is not getting called.

    Here is a console output:

    Starting /home/endre/Prg/QtCreator/build-Test1-Desktop-Default/Test1...
    ctor
    run()
    emit Error opening 'kernels/all.cl'
    /home/endre/Prg/QtCreator/build-Test1-Desktop-Default/Test1 exited with code 0

    Why is the MyWidget::clError() slot not called?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      Did you check the return value from connect ?
      qDebug() << "i work:" << connect(&worker, SIGNAL(error()), this, SLOT(clError()));
      also dont clError take parameters ?
      so it should be
      connect(&worker, SIGNAL(error( const std::string )), this, SLOT(clError( const std::string)));
      ?
      you should check out new syntax
      http://wiki.qt.io/New_Signal_Slot_Syntax

      it catches compile time connect errors.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Andy32
        wrote on last edited by
        #3

        Thanks for the tip.

        I have tried with the new connect syntax, but it didn't solve it:

        connect(&worker, &Worker::error, this, &MyWidget::clError);
        connect(this, &MyWidget::start, &worker, &Worker::run);

        I'll try qDebug() soon.
        By the way do you know how to enable qDebug in a cmake project?

        mrjjM 1 Reply Last reply
        0
        • A Andy32

          Thanks for the tip.

          I have tried with the new connect syntax, but it didn't solve it:

          connect(&worker, &Worker::error, this, &MyWidget::clError);
          connect(this, &MyWidget::start, &worker, &Worker::run);

          I'll try qDebug() soon.
          By the way do you know how to enable qDebug in a cmake project?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Andy32
          Hi
          ok, did you tried with Qt:QueuedConnection
          (last param in connect)
          You seems to be sending across threads as QWidget must live in main thread but i didnt study code in details.
          It should be automatic but its worth testing.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Andy32
            wrote on last edited by
            #5

            Hi,

            With Qt::QueuedConnection it is still not working.

            std::cout << "error connect: "
            << connect(&worker, &Worker::error, this, &MyWidget::clError, Qt::QueuedConnection) << "\n" << std::flush;
            std::cout << "start connect: "
            << connect(this, &MyWidget::start, &worker, &Worker::run, Qt::QueuedConnection) << "\n" << std::flush;

            mrjjM kshegunovK 2 Replies Last reply
            0
            • A Andy32

              Hi,

              With Qt::QueuedConnection it is still not working.

              std::cout << "error connect: "
              << connect(&worker, &Worker::error, this, &MyWidget::clError, Qt::QueuedConnection) << "\n" << std::flush;
              std::cout << "start connect: "
              << connect(this, &MyWidget::start, &worker, &Worker::run, Qt::QueuedConnection) << "\n" << std::flush;

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              Ok. odd. You seems to not overridden qthread run so msg loop should be running.
              It didn't complain about std::string as parameter ?
              (i assume connect says true)

              Sorry i cant guess what could be wrong. looks ok from quick look.
              seems to be close to
              https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

              A 1 Reply Last reply
              1
              • A Andy32

                Hi,

                With Qt::QueuedConnection it is still not working.

                std::cout << "error connect: "
                << connect(&worker, &Worker::error, this, &MyWidget::clError, Qt::QueuedConnection) << "\n" << std::flush;
                std::cout << "start connect: "
                << connect(this, &MyWidget::start, &worker, &Worker::run, Qt::QueuedConnection) << "\n" << std::flush;

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                Create the worker object in the heap and use the Qt 5 connect syntax so you get compile-time checking. In particular this line:

                connect(&worker, SIGNAL(error()), this, SLOT(clError()));
                

                is wrong. The method signatures do not match what you've given to QObject::connect.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  Ok. odd. You seems to not overridden qthread run so msg loop should be running.
                  It didn't complain about std::string as parameter ?
                  (i assume connect says true)

                  Sorry i cant guess what could be wrong. looks ok from quick look.
                  seems to be close to
                  https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                  A Offline
                  A Offline
                  Andy32
                  wrote on last edited by
                  #8

                  @mrjj said in Problem with interthread signals:

                  It didn't complain about std::string as parameter ?
                  (i assume connect says true)

                  I have changed std::string into QString and now it is working. connect() returns 1 in both cases. Is there any description about what is good or not good as signal/slot signatures? E.g. can I uses enums?

                  kshegunovK 1 Reply Last reply
                  1
                  • A Andy32

                    @mrjj said in Problem with interthread signals:

                    It didn't complain about std::string as parameter ?
                    (i assume connect says true)

                    I have changed std::string into QString and now it is working. connect() returns 1 in both cases. Is there any description about what is good or not good as signal/slot signatures? E.g. can I uses enums?

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #9

                    @Andy32 said in Problem with interthread signals:

                    Is there any description about what is good or not good as signal/slot signatures?

                    http://doc.qt.io/qt-5/custom-types.html

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    2

                    • Login

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