Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to identify sender?

How to identify sender?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    high123_98
    wrote on last edited by
    #1

    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

    jsulmJ 2 Replies Last reply
    0
    • H high123_98

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • H high123_98

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved