網(wǎng)站開(kāi)發(fā)checklist如何讓百度收錄自己信息
sender函數(shù)原型:
QObject *sender() const;
?如果在由信號(hào)激活的插槽中調(diào)用該函數(shù),返回指向發(fā)送信號(hào)的對(duì)象的指針,否則返回0,該指針僅在從該對(duì)象的線程上下文調(diào)用此函數(shù)的槽執(zhí)行期間有效。
主要代碼如下:
其中運(yùn)用了QList類直接foreach循環(huán)連接槽函數(shù)或者每個(gè)按鈕都連接
QList<QPushButton *> btnColor; //此代碼寫(xiě)入MainWindow.h文件中btnColor << ui->btn_1 << ui->btn_2 << ui->btn_3 ;
foreach (QPushButton *btn, btnColor) {connect(btn, SIGNAL(clicked(bool)), this, SLOT(changeColor()));
}//connect(ui->btn_1, &QPushButton::clicked, this, &changeColor);
//connect(ui->btn_2, &QPushButton::clicked, this, &changeColor);
//connect(ui->btn_3, &QPushButton::clicked, this, &changeColor);//槽函數(shù)
void MainWindow::changeColor()
{QPushButton *pBtn = (QPushButton*)sender();QMessageBox::about(this, "tips", pBtn->text());int index = btnColor.indexOf(pBtn);qDebug() << "index == " << index ;}
mainWindow.ui
結(jié)果:每個(gè)按鍵對(duì)應(yīng)著每個(gè)按鍵的截圖;index就是按照上面的btnColor依次排序,btn_1的序號(hào)為0,btn_2的序號(hào)為1,btn_3的序號(hào)為2.
參考:Qt sender()用法詳解-CSDN博客