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?
Forum Updated to NodeBB v4.3 + New Features

How to identify sender?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.9k 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 8 Nov 2016, 07:51 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

    J 2 Replies Last reply 8 Nov 2016, 08:02
    0
    • H high123_98
      8 Nov 2016, 07:51

      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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 8 Nov 2016, 08:02 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
        8 Nov 2016, 07:51

        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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 8 Nov 2016, 08:06 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

        3/3

        8 Nov 2016, 08:06

        • Login

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