c++qt

How to check if any of buttons is clicked in Qt


I have two buttons: zeroButton and oneButton I create a list of them and want to display "click" on a label when one of buttons is clicked. Im trying to check if one of them is clicked by contains() and mb its not necessary (without it its also doesn't work) but I cast qobject into qpushbutton with qobject_cast<QPushButton*>(sender())

I tried this:

QList<QPushButton*> *buttons = new QList<QPushButton*>();
    buttons->append(ui->zeroButton);
    buttons->append(ui->oneButton);
    
    if(buttons->contains(qobject_cast<QPushButton*>(sender()))))
    {
        ui->labelAnswer->setText("click");
    }

but its don't work. I expect that labelAnswer will be changed on "click"

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QList>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

 private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QList<QPushButton*> *buttons = new QList<QPushButton*>();
    buttons->append(ui->zeroButton);
    buttons->append(ui->oneButton);

    if(buttons->contains(sender()))
    {
        //QObject *object = new QObject(sender());
        ui->labelAnswer->setText("click");
        //delete object;
        //object = nullptr;
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp:

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="enabled">
   <bool>true</bool>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>180</width>
    <height>272</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="minimumSize">
   <size>
    <width>180</width>
    <height>272</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>180</width>
    <height>272</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="labelAnswer">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>141</width>
      <height>41</height>
     </rect>
    </property>
    <property name="autoFillBackground">
     <bool>true</bool>
    </property>
    <property name="text">
     <string>cum</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter</set>
    </property>
   </widget>
   <widget class="QPushButton" name="clearButton">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>170</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>C</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="oneButton">
    <property name="enabled">
     <bool>true</bool>
    </property>
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>50</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>1</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="twoButton">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>50</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>2</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="threeButton">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>50</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>3</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="plusButton">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>170</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>+</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="fourButton">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>90</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>4</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="fiveButton">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>90</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>5</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="sixButton">
    <property name="geometry">
     <rect>
      <x>121</x>
      <y>90</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>6</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="sevenButton">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>130</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>7</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="eightButton">
    <property name="geometry">
     <rect>
      <x>70</x>
      <y>130</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>8</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="nineButton">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>130</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>9</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
   <widget class="QPushButton" name="zeroButton">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>170</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>0</string>
    </property>
    <attribute name="buttonGroup">
     <string notr="true">buttonGroup</string>
    </attribute>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>180</width>
     <height>24</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
 <buttongroups>
  <buttongroup name="buttonGroup"/>
 </buttongroups>
</ui>

Solution

  • You can use QButtonGroup as mentioned above, here is an example:

    QPushButton b1("1");
    QPushButton b2("2");
    QButtonGroup g;
    b1.show();
    b2.show();
    g.addButton(&b1,1);
    g.addButton(&b2,2);
    QObject::connect(&g, &QButtonGroup::idClicked, [](int id){
        qDebug()<<QString("%1 clicked").arg(id);
    });