Qt是通过信号和槽的机制进行事件传递的,当有多个不同类型、或相同类型的物件的发送信号都通过一个槽来处理的时候,需要在槽中识别出这些信号然后做相应的处理。

例如:

在一个界面中有16个按钮(QPushButton)和4个(QRadioButton)这20个物件的SIGNAL(clicked(bool))都连接(connect)到同一个按键的处理槽中(void get_keyvalue(bool))

那么就需要在get_keyvalue这个槽中把这些信号的发送者都识别出来,然后取其相应的键值然后发送,其方法是:

  • void FBx::get_keyvalue(bool)
  • {
  •     if (QPushButton* btn = dynamic_cast<QPushButton*>(sender())){
  •         send_key(btn->whatsThis());
  •     }
  •     else if (QRadioButton *rtn = dynamic_cast<QRadioButton*>(sender())){
  •         send_key(rtn->whatsThis());
  •     }
  • }
  •  

    在槽(SLOT)中sender()函数会返回一个指向QObject 的指针来指向信号的发送者(Returns a pointer to the object that sent the signal, if called in a slot activated by a signal;)。然后通过C++ RTTI(Run-Time Type Identification)机制提供的dynamic_cast运算符,在执行的时候检查sender()返回的对象是否是QPushButton类,如果是则将sender()返回的QObject指针转换为QPushButton指针,然后if中的语句就会执行。如果sender()返回的对象不是QPushButton类型的指针,则dynamic_cast就会返回0,if中的语句就不会执行了。 

    相关文章:

    • 2021-08-11
    • 2021-08-30
    • 2021-11-20
    • 2021-05-14
    • 2021-08-20
    • 2021-06-07
    • 2022-01-11
    猜你喜欢
    • 2022-12-23
    • 2021-10-24
    • 2022-12-23
    • 2022-12-23
    • 2021-08-15
    • 2022-12-23
    • 2022-01-09
    相关资源
    相似解决方案