Unsolved How to identify sender?
-
Hi,
My program has two buttons, one checkbox, and one text box. I used the connect() to tie the button press events of both buttons to the click slot of the checkbox. But how can the program identify which button triggered the click event? I tried to use QObject* obj=sender(), but obj always returns the checkBox object.
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton, SIGNAL(pressed()), ui->checkBox, SLOT(click())); connect(ui->pushButton_2, SIGNAL(pressed()), ui->checkBox, SLOT(click())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_checkBox_clicked() { QObject* obj = sender(); if(obj==ui->pushButton) ui->lineEdit->text()="pushButton1"; if(obj==ui->pushButton_2) ui->lineEdit->text()="pushButton2"; }
thanks,
-GL -
@high123_98 It cannot work the way you do it: the clicked signal will always come from the checkbox. So, sender() will always return the checkbox as it is the one emitting the signal.
-
@high123_98 You can use a lambda instead of directly connecting pressed() signal to clicked() slot. In this labda you store the information which button was pressed and then call the click() slot.