Problems with Open-Source Downloads read and

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) :
        ui(new Ui::MainWindow)
        connect(ui->pushButton, SIGNAL(pressed()), ui->checkBox, SLOT(click()));
        connect(ui->pushButton_2, SIGNAL(pressed()), ui->checkBox, SLOT(click()));
        delete ui;
    void MainWindow::on_checkBox_clicked()
        QObject* obj = sender();


  • Lifetime Qt Champion

    @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.

  • Lifetime Qt Champion

    @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.

Log in to reply