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

Qt 信號(QueuedConnection 和 DirectConnection)

Qt signals (QueuedConnection and DirectConnection)(Qt 信號(QueuedConnection 和 DirectConnection))
本文介紹了Qt 信號(QueuedConnection 和 DirectConnection)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在處理 Qt 信號時遇到問題.

I'm having trouble with Qt signals.

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

I don't understand how DirectConnection and QueuedConnection works?

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

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

推薦答案

除非您使用具有不同線程親緣關系的對象,否則您不會看到太大差異.假設您有 QObjects AB 并且它們都附加到不同的線程.A 有一個名為 somethingChanged() 的信號,B 有一個名為 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() 將實際運行在 A 的線程中.基本上,就好像發出信號直接"調用插槽方法一樣.如果 B::handleChange() 不是線程安全的,這可能會導致一些(難以定位)錯誤.至少,您錯過了額外線程的好處.

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 決定使用哪種方法),事情會變得更有趣.假設 B 的線程正在運行一個事件循環,發出信號將向 B 的事件循環發送一個事件.事件循環對事件進行排隊,并最終在控制權返回給它時調用 slot 方法(它是事件循環).這使得處理 Qt 中線程之間的通信變得非常容易(再次假設您的線程正在運行它們自己的本地事件循環).您不必擔心鎖定等問題,因為事件循環會序列化槽調用.

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 的線程關聯,請查看 QObject::moveToThread.這應該會讓你開始.

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

編輯

我應該澄清一下我的開場白.如果您指定一個排隊連接,它確實會有所不同——即使對于同一線程上的兩個對象.該事件仍然發布到線程的事件循環.因此,方法調用仍然是異步的,這意味著它可以以不可預測的方式延遲(取決于循環可能需要處理的任何其他事件).但是,如果不指定連接方法,同一個線程上的對象之間的連接會自動使用直接方法(至少在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).

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

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 欧美在线视频一区 | 国产高清免费视频 | 国产在线中文字幕 | 久久中文字幕一区 | 日韩中出 | 久久久久久久久久久福利观看 | 国产精品大全 | 逼逼视频 | 一区二区三区在线 | 欧美一级二级在线观看 | 国产乱码精品一区二三赶尸艳谈 | 99国产精品99久久久久久粉嫩 | 91偷拍精品一区二区三区 | 日韩成人在线电影 | 国产在线视频一区二区董小宛性色 | 国产亚洲欧美另类一区二区三区 | jizjizjiz中国护士18 | 国产人久久人人人人爽 | 日韩中文字幕2019 | 国产伦精品 | 夜夜久久| 国产精品免费在线 | 最新超碰 | 亚洲一区二区三区观看 | 成人午夜免费网站 | 久久一区视频 | 色婷婷综合在线观看 | 久久精品一区 | 四虎av电影| 亚洲精品乱码久久久久久按摩 | 91视频网址 | 97精品国产97久久久久久免费 | 欧美不卡视频一区发布 | 亚洲欧美中文日韩在线 | 国产一区二区高清在线 | 91精品国产综合久久久久久丝袜 | 久久天天躁狠狠躁夜夜躁2014 | 国产一级电影网 | 亚洲免费视频播放 | 午夜网址 | 国产免费a |