久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Qt 信號(hào)(QueuedConnection 和 DirectConnection)

Qt signals (QueuedConnection and DirectConnection)(Qt 信號(hào)(QueuedConnection 和 DirectConnection))
本文介紹了Qt 信號(hào)(QueuedConnection 和 DirectConnection)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我在處理 Qt 信號(hào)時(shí)遇到問(wèn)題.

I'm having trouble with Qt signals.

我不明白 DirectConnectionQueuedConnection 是如何工作的?

I don't understand how DirectConnection and QueuedConnection works?

如果有人能解釋何時(shí)使用其中的哪一個(gè)(示例代碼將不勝感激),我將不勝感激.

I'd be thankful if someone will explain when to use which of these (sample code would be appreciated).

推薦答案

除非您使用具有不同線程親緣關(guān)系的對(duì)象,否則您不會(huì)看到太大差異.假設(shè)您有 QObjects AB 并且它們都附加到不同的線程.A 有一個(gè)名為 somethingChanged() 的信號(hào),B 有一個(gè)名為 handleChange() 的槽.

You won't see much of a difference unless you're working with objects having different thread affinities. Let's say you have QObjects A and B and they're both attached to different threads. A has a signal called somethingChanged() and B has a slot called handleChange().

如果您使用直接連接

connect( A, SIGNAL(somethingChanged()), B, SLOT(handleChange()), Qt::DirectConnection );

方法 handleChange() 將實(shí)際運(yùn)行在 A 的線程中.基本上,就好像發(fā)出信號(hào)直接"調(diào)用插槽方法一樣.如果 B::handleChange() 不是線程安全的,這可能會(huì)導(dǎo)致一些(難以定位)錯(cuò)誤.至少,您錯(cuò)過(guò)了額外線程的好處.

the method handleChange() will actually run in the A's thread. Basically, it's as if emitting the signal calls the slot method "directly". If B::handleChange() isn't thread-safe, this can cause some (difficult to locate) bugs. At the very least, you're missing out on the benefits of the extra thread.

如果您將連接方法更改為 Qt::QueuedConnection(或者,在這種情況下,讓 Qt 決定使用哪種方法),事情會(huì)變得更有趣.假設(shè) B 的線程正在運(yùn)行一個(gè)事件循環(huán),發(fā)出信號(hào)將向 B 的事件循環(huán)發(fā)送一個(gè)事件.事件循環(huán)對(duì)事件進(jìn)行排隊(duì),并最終在控制權(quán)返回給它時(shí)調(diào)用 slot 方法(它是事件循環(huán)).這使得處理 Qt 中線程之間的通信變得非常容易(再次假設(shè)您的線程正在運(yùn)行它們自己的本地事件循環(huán)).您不必?fù)?dān)心鎖定等問(wèn)題,因?yàn)槭录h(huán)會(huì)序列化槽調(diào)用.

If you change the connection method to Qt::QueuedConnection (or, in this case, let Qt decide which method to use), things get more interesting. Assuming B's thread is running an event loop, emitting the signal will post an event to B's event loop. The event loop queues the event, and eventually invokes the slot method whenever control returns to it (it being the event loop). This makes it pretty easy to deal with communication between/among threads in Qt (again, assuming your threads are running their own local event loops). You don't have to worry about locks, etc. because the event loop serializes the slot invocations.

注意:如果您不知道如何更改 QObject 的線程關(guān)聯(lián),請(qǐng)查看 QObject::moveToThread.這應(yīng)該會(huì)讓你開(kāi)始.

Note: If you don't know how to change a QObject's thread affinity, look into QObject::moveToThread. That should get you started.

編輯

我應(yīng)該澄清一下我的開(kāi)場(chǎng)白.如果您指定一個(gè)排隊(duì)連接,它確實(shí)會(huì)有所不同——即使對(duì)于同一線程上的兩個(gè)對(duì)象.該事件仍然發(fā)布到線程的事件循環(huán).因此,方法調(diào)用仍然是異步的,這意味著它可以以不可預(yù)測(cè)的方式延遲(取決于循環(huán)可能需要處理的任何其他事件).但是,如果不指定連接方法,同一個(gè)線程上的對(duì)象之間的連接會(huì)自動(dòng)使用直接方法(至少在Qt 4.8中是這樣).

I should clarify my opening sentence. It does make a difference if you specify a queued connection - even for two objects on the same thread. The event is still posted to the thread's event loop. So, the method call is still asynchronous, meaning it can be delayed in unpredictable ways (depending on any other events the loop may need to process). However, if you don't specify a connection method, the direct method is automatically used for connections between objects on the same thread (at least it is in Qt 4.8).

這篇關(guān)于Qt 信號(hào)(QueuedConnection 和 DirectConnection)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 哦┅┅快┅┅用力啊┅aps | 国产精品国产三级国产 | 四虎影视最新网址 | av黄色网址 | 黄色一级生活片 | 免费一级a毛片 | 亚洲一区久久 | 亚洲视频在线视频 | 岛国av噜噜噜久久久狠狠av | 黄色片免费观看 | 国产黄色免费观看 | 色污污| 午夜av在线播放 | 一区在线观看 | 国产美女自拍 | 一级黄色片视频 | 免费看黄色一级片 | 日本理论片午伦夜理片在线观看 | 国产一区二区网站 | 欧美日韩中文字幕在线观看 | 亚洲一级黄色片 | 国产精品一区二区三区不卡 | 亚洲黄色小视频 | 久久日韩精品 | 欧美精品二区三区四区免费看视频 | 日韩av在线不卡 | 午夜视频在线播放 | 欧美性猛交一区二区三区精品 | 免费黄色片视频 | www.久久爱| 国产美女免费 | 亚洲综合伊人 | 在线观看黄| 亚洲精品在线看 | 天天色视频 | 亚洲经典一区二区三区 | 99久久久 | 91片黄在线观看 | 18在线观看网站 | 美丽的姑娘观看在线播放 | 亚洲免费专区 |