如何检查Qt中是否有任何按钮被单击

问题描述 投票:0回答:1

我有两个按钮:

zeroButton
oneButton
我创建了它们的列表,并希望在单击其中一个按钮时在标签上显示“单击”。我试图检查其中一个是否被
contains()
单击,并且 mb 没有必要(没有它它也不起作用),但我用
qobject_cast<QPushButton*>(sender())

将 qobject 转换为 qpushbutton

我试过这个:

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");
    }

但它不起作用。 我希望“点击”时

labelAnswer
会发生变化

主窗口.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

主窗口.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();
}

主窗口.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>
c++ qt qt-creator
1个回答
0
投票

您可以使用QButtonGroup:

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);
});
© www.soinside.com 2019 - 2024. All rights reserved.