代码之家  ›  专栏  ›  技术社区  ›  sorush-r

如何获得带有信号/插槽机制的发送器小部件?

qt4
  •  52
  • sorush-r  · 技术社区  · 14 年前

    可以将多个信号绑定到一个插槽(不是吗?)。那么,有没有办法理解哪个小部件发送信号?我在找类似的东西 sender .NET中事件的参数

    3 回复  |  直到 14 年前
        1
  •  82
  •   Fredrick Gauss    6 年前

    QObject::sender() 会完成任务的。

        2
  •  118
  •   mitjap    8 年前

    使用 QObject::sender()

    void MainWindow::someSetupFunction( void )
    {
       ...
       connect( _foobarButton, SIGNAL(clicked()), this, SLOT(buttonPressedSlot()) );
    }
    
    void MainWindow::buttonPressedSlot()
    {
       // e.g. check with member variable _foobarButton
       QObject* obj = sender();
       if( obj == _foobarButton )
       { 
          ...
       }
    
       // e.g. casting to the class you know its connected with
       QPushButton* button = qobject_cast<QPushButton*>(sender());
       if( button != NULL ) 
       { 
          ...
       }
    
    }
    
        3
  •  7
  •   Arnold Spence    9 年前

    是的,您可以将多个信号连接到一个插槽。在这种情况下,您将使用 QSignalMapper here .