zoukankan      html  css  js  c++  java
  • 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.

    ConstantValueDescription
    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().

    http://blog.csdn.net/seanyxie/article/details/7025183


    qDebug()<<"from thread slot_main:" <<currentThreadId(); 打印当前threadID
  • 相关阅读:
    埋点和用户画像
    HDR 2 数据集预处理
    HDR 1(介绍)
    关于AR
    Android驱动
    修改用户登陆次数
    使用plsql developer报错
    oracle客户端卸载
    项目:搜索查找
    使用BeautifulSoup模块解析HTML(文件example.html)
  • 原文地址:https://www.cnblogs.com/Pymcj/p/6672028.html
Copyright © 2011-2022 走看看