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. Signal and Slot Two Threads

Signal and Slot Two Threads

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 8.9k 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.
  • S Offline
    S Offline
    sirblack
    wrote on last edited by
    #1

    I have a Problem. I am using 2 Threads, so that my GUI cannot lock up.

    I start both threads and htop is showing me two threads.

    For now, I force my working thread to enter an infinite loop once a slot is triggered.

    In my GUI Thread I emit the corresponding signal, but the whole program locks up.

    I have connected the two threads like this.

    @ connect(this, SIGNAL(setswitch(QString,int)), this->thread2, SLOT(setswitch(QString, int)), Qt::QueuedConnection);
    @

    When I change the signal and slot from QString to char* the main thread stays alive, just like expected.
    When I printed the char* in the working thread before entering the infinite loop, it only shows bogus.

    What did I miss? Why can I not connect between the two threads using the QString.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      snospmis_eht
      wrote on last edited by
      #2

      could you provide more code? how do you create those threads?

      maybe http://doc.qt.nokia.com/master-snapshot/thread-basics.html helps, there are some well explained examples.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sirblack
        wrote on last edited by
        #3

        I inherited from QThread and reimplemented run like this

        @void thread_class::run(){
        std::cout << "Starting Thread 2" <<std::endl;
        this->exec();
        }@

        in the main Thread I start the second thread like this.

        @ thread = new thread_class();
        thread->start();@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          And the object that actually implements the slot with the infinate loop? Where do you create it, and what else do you do with it?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sirblack
            wrote on last edited by
            #5

            The thread_class is the object that I send to the infinite loop.

            @class thread_class : public QThread
            {
            Q_OBJECT
            public:
            thread_class();
            void run();

            public slots:
            void setswitch(QString board, int num);

            signals:

            private:
            };@

            The slot is implemented like this.

            @void thread_class::setswitch(QString board, int num){
            std::cout << qPrintable(board) << std::endl;
            while(1);
            }@

            I just create this thread inside another class.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              You are doing it wrong (TM)

              See the excellent "article":http://developer.qt.nokia.com/wiki/Threads_Events_QObjects on the Wiki on how to deal with Threads, objects, signals & slots and events.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sirblack
                wrote on last edited by
                #7

                Oh ok, thanks. I see that its not working since this and this->thread are Objects in the same thread.
                So I have changed it like this.

                @class Worker : public QObject
                {
                Q_OBJECT

                public slots:
                void doWork() {
                /* ... */
                }
                };

                /* ... */
                QThread thread;
                Worker worker;
                connect(obj, SIGNAL(workReady()), &worker, SLOT(doWork()));
                worker.moveToThread(&thread);
                thread.start();@

                So I have my class that does the work and I move this instance to the second thread. Now the Objects are no longer in the same thread and the QueuedConnection works.

                Thank you.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Indeed. You have to remember that QThread is not a thread, it only manages one.

                  QThread can be subclassed (and it can make sense to do so), but you have to remember that only that what happens in the run() method actually happens in the other thread. Everything else happens in the thread that your QThread itself lives in. New signals and slots should be about the management of the thread, not about the actual work it is supposed to do.

                  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