qcheckbox.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_qcheckboxtest.h"
class QCheckBoxTest : public QMainWindow
{
Q_OBJECT
public:
QCheckBoxTest(QWidget *parent = Q_NULLPTR);
public slots:
void Click(bool check);
void Toggle(bool check);
void Test();
void GClick(QAbstractButton *);
void GToggle(QAbstractButton *, bool);
private:
Ui::QCheckBoxTestClass ui;
};
qcheckbox.cpp
#include "qcheckboxtest.h"
#include <QDebug>
#include <QButtonGroup>
QCheckBoxTest::QCheckBoxTest(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QButtonGroup* group = new QButtonGroup(this);
//加入组之后默认单选
group->addButton(ui.checkBox);
group->addButton(ui.checkBox_2);
group->addButton(ui.checkBox_3);
//改为多选
group->setExclusive(true);
//手动点击事件
QObject::connect(group, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(GClick(QAbstractButton *)));
//状态触发事件
QObject::connect(group, SIGNAL(buttonToggled(QAbstractButton*,bool)), this, SLOT(GToggle(QAbstractButton*,bool)));
}
void QCheckBoxTest::Click(bool check)
{
qDebug() << "Click = " << check;
}
void QCheckBoxTest::Toggle(bool check)
{
qDebug() << "Toggle = " << check;
}
void QCheckBoxTest::Test()
{
ui.checkBox_7->setChecked(!ui.checkBox_7->isChecked()); //如果勾选了就取消勾选,否则勾选
}
void QCheckBoxTest::GClick(QAbstractButton* but)
{
qDebug() << "GClick:" << but << but->isChecked();
}
void QCheckBoxTest::GToggle(QAbstractButton* but, bool check)
{
qDebug() << "GToggle:" << but << check;
}
ui_qcheckboxtest.h
/********************************************************************************
** Form generated from reading UI file 'qcheckboxtest.ui'
**
** Created by: Qt User Interface Compiler version 5.14.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_QCHECKBOXTEST_H
#define UI_QCHECKBOXTEST_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_QCheckBoxTestClass
{
public:
QWidget *centralWidget;
QCheckBox *checkBox;
QCheckBox *checkBox_2;
QCheckBox *checkBox_3;
QWidget *widget;
QCheckBox *checkBox_4;
QCheckBox *checkBox_5;
QCheckBox *checkBox_6;
QCheckBox *checkBox_7;
QPushButton *pushButton;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *QCheckBoxTestClass)
{
if (QCheckBoxTestClass->objectName().isEmpty())
QCheckBoxTestClass->setObjectName(QString::fromUtf8("QCheckBoxTestClass"));
QCheckBoxTestClass->resize(600, 400);
centralWidget = new QWidget(QCheckBoxTestClass);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
checkBox = new QCheckBox(centralWidget);
checkBox->setObjectName(QString::fromUtf8("checkBox"));
checkBox->setGeometry(QRect(110, 40, 91, 19));
checkBox->setAutoExclusive(true);
checkBox_2 = new QCheckBox(centralWidget);
checkBox_2->setObjectName(QString::fromUtf8("checkBox_2"));
checkBox_2->setGeometry(QRect(110, 80, 91, 19));
checkBox_2->setAutoExclusive(true);
checkBox_3 = new QCheckBox(centralWidget);
checkBox_3->setObjectName(QString::fromUtf8("checkBox_3"));
checkBox_3->setGeometry(QRect(110, 120, 91, 19));
checkBox_3->setAutoExclusive(true);
widget = new QWidget(centralWidget);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(280, 70, 191, 161));
checkBox_4 = new QCheckBox(widget);
checkBox_4->setObjectName(QString::fromUtf8("checkBox_4"));
checkBox_4->setGeometry(QRect(10, 30, 91, 19));
checkBox_4->setAutoExclusive(true);
checkBox_5 = new QCheckBox(widget);
checkBox_5->setObjectName(QString::fromUtf8("checkBox_5"));
checkBox_5->setGeometry(QRect(10, 70, 91, 19));
checkBox_5->setAutoExclusive(true);
checkBox_6 = new QCheckBox(widget);
checkBox_6->setObjectName(QString::fromUtf8("checkBox_6"));
checkBox_6->setGeometry(QRect(10, 110, 91, 19));
checkBox_6->setAutoExclusive(true);
checkBox_7 = new QCheckBox(centralWidget);
checkBox_7->setObjectName(QString::fromUtf8("checkBox_7"));
checkBox_7->setGeometry(QRect(180, 270, 91, 19));
pushButton = new QPushButton(centralWidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(60, 270, 93, 28));
QCheckBoxTestClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(QCheckBoxTestClass);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 600, 26));
QCheckBoxTestClass->setMenuBar(menuBar);
mainToolBar = new QToolBar(QCheckBoxTestClass);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
QCheckBoxTestClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(QCheckBoxTestClass);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
QCheckBoxTestClass->setStatusBar(statusBar);
retranslateUi(QCheckBoxTestClass);
QObject::connect(checkBox_7, SIGNAL(toggled(bool)), QCheckBoxTestClass, SLOT(Toggle(bool)));
QObject::connect(pushButton, SIGNAL(clicked()), QCheckBoxTestClass, SLOT(Test()));
QObject::connect(checkBox_7, SIGNAL(clicked(bool)), QCheckBoxTestClass, SLOT(Click(bool)));
QMetaObject::connectSlotsByName(QCheckBoxTestClass);
} // setupUi
void retranslateUi(QMainWindow *QCheckBoxTestClass)
{
QCheckBoxTestClass->setWindowTitle(QCoreApplication::translate("QCheckBoxTestClass", "QCheckBoxTest", nullptr));
checkBox->setText(QCoreApplication::translate("QCheckBoxTestClass", "CheckBox", nullptr));
checkBox_2->setText(QCoreApplication::translate("QCheckBoxTestClass", "CheckBox", nullptr));
checkBox_3->setText(QCoreApplication::translate("QCheckBoxTestClass", "CheckBox", nullptr));
checkBox_4->setText(QCoreApplication::translate("QCheckBoxTestClass", "CheckBox", nullptr));
checkBox_5->setText(QCoreApplication::translate("QCheckBoxTestClass", "CheckBox", nullptr));
checkBox_6->setText(QCoreApplication::translate("QCheckBoxTestClass", "CheckBox", nullptr));
checkBox_7->setText(QCoreApplication::translate("QCheckBoxTestClass", "CheckBox", nullptr));
pushButton->setText(QCoreApplication::translate("QCheckBoxTestClass", "PushButton", nullptr));
} // retranslateUi
};
namespace Ui {
class QCheckBoxTestClass: public Ui_QCheckBoxTestClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_QCHECKBOXTEST_H