QCheckBox
1.QAbstractButton 支持快捷键
2.text() setText()
3.bool isChecked() const
4.void setChecked(bool) toggled(bool)
QCheckBox 多选和单选
1.setAutoExclusive
QCheckBox 事件
1.clicked(bool)
2.toggled(bool)
QCheckBox
1.QAbstractButton 支持快捷键
2.text() setText()
3.bool isChecked() const
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QCheckBox *checkBox_1;
QCheckBox *checkBox_2;
QCheckBox *checkBox_3;
QWidget *widget;
QCheckBox *checkBox_6;
QCheckBox *checkBox_5;
QCheckBox *checkBox_4;
QCheckBox *checkBox_7;
QPushButton *pushButton;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
checkBox_1 = new QCheckBox(Widget);
checkBox_1->setObjectName(QString::fromUtf8("checkBox_1"));
checkBox_1->setGeometry(QRect(120, 20, 141, 23));
checkBox_1->setAutoExclusive(true);
checkBox_2 = new QCheckBox(Widget);
checkBox_2->setObjectName(QString::fromUtf8("checkBox_2"));
checkBox_2->setGeometry(QRect(120, 60, 121, 23));
checkBox_2->setAutoExclusive(true);
checkBox_3 = new QCheckBox(Widget);
checkBox_3->setObjectName(QString::fromUtf8("checkBox_3"));
checkBox_3->setGeometry(QRect(120, 100, 101, 23));
checkBox_3->setAutoExclusive(true);
widget = new QWidget(Widget);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(340, 60, 281, 161));
checkBox_6 = new QCheckBox(widget);
checkBox_6->setObjectName(QString::fromUtf8("checkBox_6"));
checkBox_6->setGeometry(QRect(100, 60, 121, 23));
checkBox_6->setAutoExclusive(true);
checkBox_5 = new QCheckBox(widget);
checkBox_5->setObjectName(QString::fromUtf8("checkBox_5"));
checkBox_5->setGeometry(QRect(100, 20, 141, 23));
checkBox_5->setAutoExclusive(true);
checkBox_4 = new QCheckBox(widget);
checkBox_4->setObjectName(QString::fromUtf8("checkBox_4"));
checkBox_4->setGeometry(QRect(100, 100, 101, 23));
checkBox_4->setAutoExclusive(true);
checkBox_7 = new QCheckBox(Widget);
checkBox_7->setObjectName(QString::fromUtf8("checkBox_7"));
checkBox_7->setGeometry(QRect(80, 320, 92, 23));
pushButton = new QPushButton(Widget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(300, 320, 89, 25));
retranslateUi(Widget);
QObject::connect(checkBox_7, SIGNAL(clicked(bool)), Widget, SLOT(Click(bool)));
QObject::connect(checkBox_7, SIGNAL(toggled(bool)), Widget, SLOT(Toggle(bool)));
QObject::connect(pushButton, SIGNAL(clicked()), Widget, SLOT(Text()));
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
checkBox_1->setText(QCoreApplication::translate("Widget", "CheckBox1", nullptr));
checkBox_2->setText(QCoreApplication::translate("Widget", "CheckBox2", nullptr));
checkBox_3->setText(QCoreApplication::translate("Widget", "CheckBox3", nullptr));
checkBox_6->setText(QCoreApplication::translate("Widget", "CheckBox5", nullptr));
checkBox_5->setText(QCoreApplication::translate("Widget", "CheckBox4", nullptr));
checkBox_4->setText(QCoreApplication::translate("Widget", "CheckBox6", nullptr));
checkBox_7->setText(QCoreApplication::translate("Widget", "CheckBox", nullptr));
pushButton->setText(QCoreApplication::translate("Widget", "Test", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
wiget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::Click(bool check)
{
qDebug()<<"Click = "<<check;
}
void Widget::Toggle(bool check)
{
qDebug()<<"Toggle = "<<check;
}
void Widget::Text()
{
ui->checkBox_7->setChecked(!ui->checkBox_7->isChecked());
}
QButtonGroup
1.setExclusive
2.void buttonClicked(QAbstractButton *button)
3.void buttonClicked(int id)
4.void buttonPressed(QAbstractButton *button)
5.void buttonReleased(QAbstractButton *button)
6.void buttonPressed(int id);
7.void buttonReleased(int id);
8.void buttonToggled(QAbstractButton *button,bool checked);
9.void buttonToggled(int id,bool checked);
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QButtonGroup *group = new QButtonGroup(this);
//加组默认单选
group->addButton(ui->checkBox1);
group->addButton(ui->checkBox2);
group->addButton(ui->checkBox3);
//改为多选
//group->setExclusive(false);
//手动点击事件
QObject::connect(group,&QButtonGroup::buttonClicked,this,&Widget::GClick);
//状态变化触发事件
QObject::connect(group,&QButtonGroup::buttonToggled,this,&Widget::GToggled);
}
void Widget::GClick(QAbstractButton *but)
{
qDebug()<<"GClick "<<but<<but->isChecked();
}
void Widget::GToggled(QAbstractButton *but)
{
qDebug()<<"GToggled "<<but<<but->isChecked();
}
Widget::~Widget()
{
delete ui;
}