天天看點

多個QPushbutton綁定統一個槽函數

多個QPushbutton綁定同一個槽函數,槽函數通過sender判斷點選的按鈕。

int i = 0;
QString str("pushButton %1");
QPushButton* pushButton;
for (i = 0; i<16; ++i)
{
    pushButton = new QPushButton(str.arg(i + 1), widget);
    pushButton->setMinimumSize(pushButton->size());
    this->m_pSCVLayout->addWidget(pushButton);
    pushButton->setObjectName(QString::number(i+1));

    
    connect(pushButton, SIGNAL(clicked()), this, SLOT(OnButrtonClickSlot2()));
}      
void OnButrtonClickSlot2()
{
    QPushButton* btn = (QPushButton*)sender();
    qDebug() << "id:" << btn->objectName();

}      
id: "1"
id: "2"
id: "3"
id: "4"
id: "5"
id: "6"
id: "7"
id: "15"
id: "16"
id: "13"
id: "11"      

繼續閱讀