Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 149559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:09:00+00:00 2026-05-11T09:09:00+00:00

Qt documentation states that signals and slots can be direct , queued and auto

  • 0

Qt documentation states that signals and slots can be direct, queued and auto.

It also stated that if object that owns slot ‘lives’ in a thread different from object that owns signal, emitting such signal will be like posting message – signal emit will return instantly and slot method will be called in target thread’s event loop.

Unfortunately, documentation do not specify that ‘lives’ stands for and no examples is available. I have tried the following code:

main.h:

class CThread1 : public QThread { Q_OBJECT public:     void run( void )     {         msleep( 200 );         std::cout << 'thread 1 started' << std::endl;         MySignal();         exec();     } signals:     void MySignal( void ); };  class CThread2 : public QThread { Q_OBJECT public:     void run( void )     {         std::cout << 'thread 2 started' << std::endl;         exec();     } public slots:     void MySlot( void )     {         std::cout << 'slot called' << std::endl;     } }; 

main.cpp:

int main(int argc, char *argv[]) {     QCoreApplication a(argc, argv);     CThread1 oThread1;     CThread2 oThread2;     QObject::connect( & oThread1, SIGNAL( MySignal() ),         & oThread2, SLOT( MySlot() ) );     oThread1.start();     oThread2.start();     oThread1.wait();     oThread2.wait();     return a.exec(); } 

Output is:

thread 2 started thread 1 started 

MySlot() is never called :(. What I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:09:00+00:00Added an answer on May 11, 2026 at 9:09 am

    There are quite a few problems with your code :

    • like said by Evan the emit keyword is missing
    • all your objects live in the main thread, only the code in the run methods live in other threads, which means that the MySlot slot would be called in the main thread and I’m not sure that’s what you want
    • your slot will never be called since the main event loop will never been launched : your two calls to wait() will only timeout after a very long time (and you’ll probably kill your application before that happens) and I don’t think that’s what you want either, anyway they really have no use in your code.

    This code would most likely work (though I have not tested it) and I think it does what you want it to do :

    class MyObject : public QObject {     Q_OBJECT public slots:     void MySlot( void )     {         std::cout << 'slot called' << std::endl;     } };  class CThread1 : public QThread {     Q_OBJECT public:     void run( void )     {         std::cout << 'thread 1 started' << std::endl;         int i = 0;         while(1)         {            msleep( 200 );            i++;            if(i==1000)               emit MySignal();         }     } signals:     void MySignal( void ); };  class CThread2 : public QThread {     Q_OBJECT public:     void run( void )     {         std::cout << 'thread 2 started' << std::endl;         exec();     } };  int main(int argc, char *argv[]) {     QCoreApplication a(argc, argv);     CThread1 oThread1;     CThread2 oThread2;     MyObject myObject;     QObject::connect( & oThread1, SIGNAL( MySignal() ),         & myObject, SLOT( MySlot() ) );     oThread2.start();     myObject.moveToThread(&oThread2)     oThread1.start();     return a.exec(); } 

    Now MyObject will live in thread2 (thanks to moveToThread).

    MySignal should be sent from thread1 (thought I’m not sure on that one, it might be sent from main thread, it doesn’t really matter).

    No event loop is needed in thread1 since emitting a signal doesn’t need an event loop. An event loop is needed in thread2 (lanched by exec()) to receive the signal.

    MySlot will be called in thread2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Documentation states that interface delegation is available for Win32 only. Currently I can't test
The rbenv documentation states that you can use the gemsets plugin to sandbox your
jQueryUI Datepicker documentation states that the minDate option can be set using a string
The PHP documentation states that php://input can only be read once. In my application
The documentation states that MATLAB can be launched in single-thread mode as follows: matlab
Documentation states that if an Intent can be processed by multiple applications the user
The Apple's documentation states that A string object is implemented as an array of
The foursquare venue search api documentation states that the limit parameter can go up
Matlab documentation states that it is possible to replace the Nth occurrence of the
Doctrine 2 documentation states that: Removing an association between two entities is similarly straight-forward.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.