使用函数:
QObject::connect
现在的参数不一定要使用宏了,可以直接传函数指针。
函数声明如下:
QMetaObject::Connection QObject::connect ( const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type = Qt::AutoConnection );
这是一个静态函数,用于绑定信号和槽,还有其他几种参数列表,差异基本就是指定发送/接收方的方式不同,比如其实还可以用字符串指定函数名。这里所说的绑定方式的关键是,最后一个参数,也就是给了默认值的:
Qt::ConnectionType type = Qt::AutoConnection
首先看一下这个参数的可选值有哪些:(来自官方说明)
Constant | Value | Description |
---|---|---|
Qt::AutoConnection | 0 | (Default) If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted. |
Qt::DirectConnection | 1 | The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread. |
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 | 3 | Same as Qt::QueuedConnection, except that the signalling thread blocks until the slot returns. This connection must not be used if the receiver lives in the signalling thread, or else the application will deadlock. |
Qt::UniqueConnection | 0x80 | This is a flag that can be combined with any one of the above connection types, using a bitwise OR. When Qt::UniqueConnection is set, QObject::connect() will fail if the connection already exists (i.e. if the same signal is already connected to the same slot for the same pair of objects). This flag was introduced in Qt 4.6. |
基本就是区别槽的执行线程是哪个。
信号可以绑定信号 信号不需要定义
槽需要定义
两者都是函数
槽的权限
public slots:在这个区内声明的槽意味着任何对象都可将信号与之相连接。 这对于组件编程非常有用,你可以创建彼此互不了解的对象, 将它们的信号与槽进行连接以便信息能够正确的传递。 protected slots:在这个区内声明的槽意味着当前类及其子类可以将信号与之相连接。 这适用于那些槽,它们是类实现的一部分,但是其界面接口却面向外部。 private slots:在这个区内声明的槽意味着只有类自己可以将信号与之相连接。 这适用于联系非常紧密的类。 槽也能够声明为虚函数,这也是非常有用的。
如何声明 信号 槽
槽在哪个线程里面运行的
Qt槽和信号的权限
Qt的emit