2010-10-28 47 views

回答

5

是的,您可以將多個信號連接到一個插槽。在這種情況下,您可以使用QSignalMapper來區分信號的來源。該解決方案僅限於無參數信號。你可以看到一個例子here

+0

鏈接是死的... – MDMoore313 2014-02-08 15:53:47

+1

固定鏈接。謝謝! – 2014-02-09 04:41:47

+0

鏈接再次死亡= [ – Troyseph 2015-05-29 14:06:29

97

使用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) 
    { 
     ... 
    } 

} 
+11

+1爲一個實際的例子。 – MDMoore313 2014-02-08 15:54:13

+1

不是qobject_cast比普通dynamic_cast更好嗎? – elephant 2015-09-24 07:07:03

+1

@ephant是的,正如http://doc.qt.io/qt-4.8/metaobjects.html上的描述。答案中應該改進。 – Johannes 2015-12-30 20:56:01