Qt Cross Thread Signal Slot

Posted on
  1. Qt Cross Thread Signal Slotted
  2. Qt Signals And Slots Tutorial
  3. Qt Connect Signal Slot

First of all, why are you using char.? This is C code with Qt, you should be using QString or std::string. Second, the code is working as expected, but you don't need signals, slots, threads, or even a UI to demonstrate this behavior. See Stack Overflow's excellent page on how to create an MCVE. – MrEricSir Feb 1 '15 at 5:28. Cross-thread signal slot, how to send char. c,multithreading,qt,signals-slots I've created a simple Qt GUI App and I've created a simple thread in it using worker and worker thread (see below codes, it is a simple code which will be used in a large program). In this program when I push the push button, the thread is created. I'm using a Qt cross-thread (QueuedConnection) signal and slot to communicate between my service thread and the main thread. The problem I am having is that although the service thread emits the signal (well it executes the instruction) the slot in the main thread doesn't get called. I've removed all of the TCP server code to simplify things.

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:

main.cpp:

Output is:

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

Answers:

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

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

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.

Answers:

Do not subclass QThread for Qt 4.4+

While Aiua’s answer is good, I want to point out some issues with QThread and Qt 4.6 or 4.7.

Qt Cross Thread Signal Slotted

This article sums it up: http://blog.qt.io/blog/2010/06/17/youre-doing-it-wrong/

Lack of Documentation on Qt’s part

Unfortunately the problem stems from a lack of updates to documentation. Prior to Qt 4.4 QThread had no default run() implementation, which meant that you had to subclass QThread in order to use it.

If you’re using Qt 4.6 or 4.7 then you almost certainly should not subclass QThread.

Use moveToThread

The key to getting slots to execute in a worker thread is to use the moveToThread method as Aiua pointed out.

Tags: qt

2020腾讯云限时秒杀,爆款1核2G云服务器99元/年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062

2020阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods

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:

main.cpp:

Output is:

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

Recommend:c++ - Signals and Slots help QT

ss lets say test. in the ui function i need to connect my signal and slot but im trying the code from the QT docs and having no luck signal declaration signals:void paint(int x, int y, int id); signal emit emit paint(x, y, id) connectio

answer 1 >>---Accepted---Accepted---Accepted---

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 :

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

Qt Signals And Slots Tutorial

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.

Recommend:c++ - How to create dynamic signals and slots in Qt

Qt Connect Signal Slot

ed by the moc compiler. Now I want to create signals and slots dynamically at run-time. I already have a working solution, but it feels to me like a hack, although I am using publicly available methods. This is the code for dynamic slots: b