Qt跨线程信号和槽的连接

Qt支持三种类型的信号-槽连接:
1,直接连接,当signal发射时,slot立即调用。此slot在发射signal的那个线程中被执行(不一定是接收对象生存的那个线程)
2,队列连接,当控制权回到对象属于的那个线程的事件循环时,slot被调用。此slot在接收对象生存的那个线程中被执行
3,自动连接(缺省),假如信号发射与接收者在同一个线程中,其行为如直接连接,否则,其行为如队列连接。

连接类型可能通过以向connect()传递参数来指定。注意的是,当发送者与接收者生存在不同的线程中,而事件循环正运行于接收者的线程中,使用直接连接是不安全的。同样的道理,调用生存在不同的线程中的对象的函数也是不是安全的。QObject::connect()本身是线程安全的。


This enum describes the types of connection that can be used between signals and slots. In particular, it determines whether a particular signal is delivered to a slot immediately or queued for delivery at a later time.

Constant Value Description
Qt::AutoConnection 0 (default) If the signal is emitted from a different thread than the receiving object, the signal is queued, behaving as Qt::QueuedConnection. Otherwise, the slot is invoked directly, behaving as Qt::DirectConnection. The type of connection is determined when the signal is emitted.
Qt::DirectConnection 1 The slot is invoked immediately, when the signal is emitted.
Qt::QueuedConnection 2 The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
Qt::BlockingQueuedConnection 4 Same as QueuedConnection, except the current thread blocks until the slot returns. This connection type should only be used where the emitter and receiver are in different threads. Note: Violating this rule can cause your application to deadlock.
Qt::UniqueConnection 0x80 Same as AutoConnection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection will fail. This connection type was introduced in Qt 4.6.
Qt::AutoCompatConnection 3 The default type when Qt 3 support is enabled. Same as AutoConnection but will also cause warnings to be output in certain situations. See Compatibility Signals and Slots for further information.

With queued connections, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. If you try to use a queued connection and get the error message:

 QObject::connect: Cannot queue arguments of type 'MyType'

Call qRegisterMetaType() to register the data type before you establish the connection.

When using signals and slots with multiple threads, see Signals and Slots Across Threads.

See also Thread Support in QtQObject::connect(), and qRegisterMetaType().


Qt中,线程信号连接需要使用Qt线程间通信机制。下面是一些基本的步骤: 1. 使用QObject::moveToThread()方法将对象移动到目标线程。 2. 在目标线程中创建一个QObject,并调用QObject::connect()方法将信号连接起来。 3. 如果信号需要传递参数,则需要使用Qt的元对象系统(Meta-Object System)来注册参数类型。可以使用Q_DECLARE_METATYPE宏来注册自定义类型。 4. 在发送信号时,需要使用QMetaObject::invokeMethod()方法,将该方法的第一个参数设置为接收信号的对象,第二个参数设置为接收信号的函数名,第三个参数设置为Qt::QueuedConnection,以确保信号被放入目标线程的事件队列中。 下面是一个简单的示例: ```cpp class Worker : public QObject { Q_OBJECT signals: void resultReady(int result); public slots: void doWork() { int result = 0; // 计算结果 emit resultReady(result); } }; class Controller : public QObject { Q_OBJECT public: Controller() { Worker *worker = new Worker(); QThread *workerThread = new QThread(); worker->moveToThread(workerThread); connect(workerThread, &QThread::started, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResult); workerThread->start(); } ~Controller() { // 停止线程 } public slots: void handleResult(int result) { // 处理结果 } }; ``` 在这个例子中,Worker对象的doWork()方法在一个新的线程中执行,当计算完成后,使用信号resultReady(int result)发送结果。Controller对象将自己的handleResult(int result)连接到该信号上,在目标线程中处理结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值