QT学习之路--深入了解信号槽

  槽函数可以和一个信号相连接,当这个信号发生时,它可以被自动调用。connect()语句的原型类似于:
connect(sender, SIGNAL(signal), receiver, SLOT(slot));

  sender 和 receiver 都是 QObject 类型的,singal 和 slot 都是没有参数名称的函数签名。SINGAL()和 SLOT()宏用于把参数转换成字符串。

  信号槽还有更多可能的用法,如下所示。

  一个信号可以和多个槽相连:
  connect(slider, SIGNAL(valueChanged(int)),spinBox, SLOT(setValue(int)));
  connect(slider, SIGNAL(valueChanged(int)),this, SLOT(updateStatusBarIndicator(int)));

注意,如果是这种情况,这些槽会一个接一个的被调用,但是它们的调用顺序是不确定的。

  多个信号可以连接到一个槽:
  connect(lcd, SIGNAL(overflow()),this, SLOT(handleMathError()));
  connect(calculator, SIGNAL(divisionByZero()),this, SLOT(handleMathError()));

这是说,只要任意一个信号发出,这个槽就会被调用。

  槽可以被取消链接:

  disconnect(lcd, SIGNAL(overflow()),this, SLOT(handleMathError()));

 

为了正确的连接信号槽,信号和槽的参数个数、类型以及出现的顺序都必须相同,
例如:
connect(ftp, SIGNAL(rawCommandReply(int, const QString &)),this, SLOT(processReply(int, const QString &)));
这里有一种例外情况,如果信号的参数多于槽的参数,那么这个参数之后的那些参数都会被忽略掉,例如:
connect(ftp, SIGNAL(rawCommandReply(int, const QString &)),this, SLOT(checkErrorCode(int)));
这里,const QString &这个参数就会被槽忽略掉。

 

posted on 2016-10-11 21:26  白宫飘红旗  阅读(233)  评论(0)    收藏  举报