Qt学习之路(9):深入了解信号槽

信号槽机制是Qt编程的基础。通过信号槽,能够使Qt各组件在不知道对方的情形下能够相互通讯。这就将类之间的关系做了最大程度的解耦。

槽函数和普通的C++成员函数没有很大的区别。它们也可以使virtual的;可以被重写;可以使public、protected或者 private的;可以由其它的C++函数调用;参数可以是任何类型的。如果要说区别,那就是,槽函数可以和一个信号相连接,当这个信号发生时,它可以被自动调用。

connect()语句的原型类似于:

[cpp]  view plain copy
  1. connect<span>(</span>sender, SIGNAL<span>(</span><span>signal</span><span>)</span>, receiver, SLOT<span>(</span>slot<span>)</span><span>)</span><span>;</span>  

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

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

一个信号可以和多个槽相连:

[cpp]  view plain copy
  1. connect<span>(</span>slider, SIGNAL<span>(</span>valueChanged<span>(</span><span>int</span><span>)</span><span>)</span>,  
  2.               spinBox, SLOT<span>(</span>setValue<span>(</span><span>int</span><span>)</span><span>)</span><span>)</span><span>;</span>  
  3. connect<span>(</span>slider, SIGNAL<span>(</span>valueChanged<span>(</span><span>int</span><span>)</span><span>)</span>,  
  4.               <span>this</span>, SLOT<span>(</span>updateStatusBarIndicator<span>(</span><span>int</span><span>)</span><span>)</span><span>)</span><span>;</span>  

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

多个信号可以连接到一个槽:

[cpp]  view plain copy
  1. connect<span>(</span>lcd, SIGNAL<span>(</span>overflow<span>(</span><span>)</span><span>)</span>,  
  2.               <span>this</span>, SLOT<span>(</span>handleMathError<span>(</span><span>)</span><span>)</span><span>)</span><span>;</span>  
  3. connect<span>(</span>calculator, SIGNAL<span>(</span>divisionByZero<span>(</span><span>)</span><span>)</span>,  
  4.               <span>this</span>, SLOT<span>(</span>handleMathError<span>(</span><span>)</span><span>)</span><span>)</span><span>;</span>  

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

一个信号可以连接到另外的一个信号:

[cpp]  view plain copy
  1. connect<span>(</span>lineEdit, SIGNAL<span>(</span>textChanged<span>(</span><span>const</span> QString <span>&</span>amp<span>;</span><span>)</span><span>)</span>,  
  2.               <span>this</span>, SIGNAL<span>(</span>updateRecord<span>(</span><span>const</span> QString <span>&</span>amp<span>;</span><span>)</span><span>)</span><span>)</span><span>;</span>  

这是说,当第一个信号发出时,第二个信号被发出。除此之外,这种信号-信号的形式和信号-槽的形式没有什么区别。

槽可以被取消链接:

[cpp]  view plain copy
  1. disconnect<span>(</span>lcd, SIGNAL<span>(</span>overflow<span>(</span><span>)</span><span>)</span>,  
  2.                  <span>this</span>, SLOT<span>(</span>handleMathError<span>(</span><span>)</span><span>)</span><span>)</span><span>;</span>  

这种情况并不经常出现,因为当一个对象delete之后,Qt自动取消所有连接到这个对象上面的槽。

为了正确的连接信号槽,信号和槽的参数个数、类型以及出现的顺序都必须相同,例如:

[cpp]  view plain copy
  1. connect<span>(</span>ftp, SIGNAL<span>(</span>rawCommandReply<span>(</span><span>int</span>, <span>const</span> QString <span>&</span>amp<span>;</span><span>)</span><span>)</span>,  
  2.               <span>this</span>, SLOT<span>(</span>processReply<span>(</span><span>int</span>, <span>const</span> QString <span>&</span>amp<span>;</span><span>)</span><span>)</span><span>)</span><span>;</span>  

这里有一种例外情况,如果信号的参数多于槽的参数,那么这个参数之后的那些参数都会被忽略掉,例如:

[cpp]  view plain copy
  1. connect<span>(</span>ftp, SIGNAL<span>(</span>rawCommandReply<span>(</span><span>int</span>, <span>const</span> QString <span>&</span>amp<span>;</span><span>)</span><span>)</span>,  
  2.             <span>this</span>, SLOT<span>(</span>checkErrorCode<span>(</span><span>int</span><span>)</span><span>)</span><span>)</span><span>;</span>  

这里,const QString &这个参数就会被槽忽略掉。

如果信号槽的参数不相容,或者是信号或槽有一个不存在,或者在信号槽的连接中出现了参数名字,在Debug模式下编译的时候,Qt都会很智能的给出警告。

在这之前,我们仅仅在widgets中使用到了信号槽,但是,注意到connect()函数其实是在QObject中实现的,并不局限于GUI,因此,只要我们继承QObject类,就可以使用信号槽机制了:

[cpp]  view plain copy
  1. <span>class</span> Employee <span>:</span> <span>public</span> QObject  
  2. <span>{</span>  
  3.         Q_OBJECT  
  4. <span>public</span><span>:</span>  
  5.         Employee<span>(</span><span>)</span> <span>{</span> mySalary <span>=</span> <span>0</span><span>;</span> <span>}</span>  
  6.         <span>int</span> salary<span>(</span><span>)</span> <span>const</span> <span>{</span> <span>return</span> mySalary<span>;</span> <span>}</span>  
  7.    
  8. <span>public</span> slots<span>:</span>  
  9.         <span>void</span> setSalary<span>(</span><span>int</span> newSalary<span>)</span><span>;</span>  
  10.    
  11. signals<span>:</span>  
  12.         <span>void</span> salaryChanged<span>(</span><span>int</span> newSalary<span>)</span><span>;</span>  
  13.    
  14. <span>private</span><span>:</span>  
  15.         <span>int</span> mySalary<span>;</span>  
  16. <span>}</span><span>;</span>  

在使用时,我们给出下面的代码:

[cpp]  view plain copy
  1. <span>void</span> Employee<span>::</span><span>setSalary</span><span>(</span><span>int</span> newSalary<span>)</span>  
  2. <span>{</span>  
  3.         <span>if</span> <span>(</span>newSalary <span>!</span><span>=</span> mySalary<span>)</span> <span>{</span>  
  4.                 mySalary <span>=</span> newSalary<span>;</span>  
  5.                 emit salaryChanged<span>(</span>mySalary<span>)</span><span>;</span>  
  6.         <span>}</span>  
  7. <span>}</span>  

这样,当setSalary()调用的时候,就会发出salaryChanged()信号。注意这里的if判断,这是避免递归的方式!还记得前面提到的循环连接吗?如果没有if,当出现了循环连接的时候就会产生无限递归。

你可能感兴趣的:(Qt学习之路(9):深入了解信号槽)