当前位置:网站首页>Qt: get sender in slot (the control that triggers signal)

Qt: get sender in slot (the control that triggers signal)

2022-06-09 02:32:00 Xiaoying Information Technology Service Department

Qt Development , One scenario is to click three different buttons , Trigger the same event (SLOT), To be in SLOT Method to distinguish three different buttons :

connect(ui->btnType1, SIGNAL(clicked()),this,SLOT(changeType2()));
connect(ui->btnType2, SIGNAL(clicked()),this,SLOT(changeType2()));
connect(ui->btnType3, SIGNAL(clicked()),this,SLOT(changeType2()));
...

void FormCL3P::changeType2()
{
   // Get the clicked button 
   //?
}

So how to SLOT Get the current sender Well ? The answer was found on the Internet .

use sender() Method :

void FormCL3P::changeType2()
{
    QObject* btn = sender();
    if (btn == ui->btnType1){ ... }
    if (btn == ui->btnType2){ ... }
    if (btn == ui->btnType3){ ... }
}

Reference resources :In qml, How can i get signal sender from slot/function? | Qt Forum

原网站

版权声明
本文为[Xiaoying Information Technology Service Department]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/159/202206081248285384.html