1、connect指定的SIGNAL和SLOT的成员函数,不能携带参数名,只能带参数类型,比如:QObject::connect(pushButton, SIGNAL(clicked(bool)), qt2Class, SLOT(setEnabled(bool)));否则,调试运行的时候,会提示的:
QObject::connect: No such signal QPushButton::clicked(int) in D:\Qt_Project\qt2\qt2\GeneratedFiles\ui_qt2.h:58
QObject::connect: (sender name: 'pushButton')
QObject::connect: (receiver name: 'qt2Class')
2、The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) 译做:SIGNAL和SLOT必须有同样的标识,实际上,SLOT可以带更少的参数,因为他会忽略多余的参数。
3、QObject派生类,指定的slot,其实就是一般的成员函数,只是能用在connect里面,必须有函数体,signal不一样,仅仅需要声明,不需要函数体,而且,可以由其他成员函数emit操作,如下:
class myButton : public QPushButton
{
Q_OBJECT
public:
myButton(QWidget *parent = NULL)
:QPushButton(parent)
{
}
void send(int value)
{
emit mysignal(value);
}
public slots:
void myslot()
{}
signals:
void mysignal(int value);
};
4、 if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro.比如有信号:
void destroyed(QObject* = 0);
有槽:
void objectDestroyed(QObject* obj = 0);
那么,可以有如下操作:
connect(sender, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(Qbject*)));
connect(sender, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed()));
connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed()));
但是不能如下:
connect(sender, SIGNAL(destroyed()), this, SLOT(objectDestroyed(QObject*)));
这个slot匹配的是一个无法发送signal的QObject对象,因为slot和signal的参数不会在编译期间被检测。