8.2、Qt::QCheckBoxTest操作

前言:

本文操作均为在vs2015+QT5.9.5版本中执行

头文件:qcheckboxtest.h

#pragma once

#include <QtWidgets/QWidget>
#include "ui_qcheckboxtest.h"

class QCheckBoxTest : public QWidget
{
	Q_OBJECT

public:
	QCheckBoxTest(QWidget *parent = Q_NULLPTR);

public slots:
	void Click(bool check);
	void Toggle(bool check);
	void Test();
private:
	Ui::QCheckBoxTestClass ui;
};

UI文件:ui_qcheckboxtest.h

/********************************************************************************
** Form generated from reading UI file 'qcheckboxtest.ui'
**
** Created by: Qt User Interface Compiler version 5.9.5
**
** 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/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_QCheckBoxTestClass
{
public:
    QWidget *widget;
    QCheckBox *checkBox6;
    QCheckBox *checkBox4;
    QCheckBox *checkBox5;
    QGroupBox *groupBox;
    QCheckBox *checkBox3;
    QCheckBox *checkBox1;
    QCheckBox *checkBox2;
    QCheckBox *checkBox7;
    QPushButton *TeseButton;

    void setupUi(QWidget *QCheckBoxTestClass)
    {
        if (QCheckBoxTestClass->objectName().isEmpty())
            QCheckBoxTestClass->setObjectName(QStringLiteral("QCheckBoxTestClass"));
        QCheckBoxTestClass->resize(813, 625);
        widget = new QWidget(QCheckBoxTestClass);
        widget->setObjectName(QStringLiteral("widget"));
        widget->setGeometry(QRect(400, 140, 211, 171));
        checkBox6 = new QCheckBox(widget);
        checkBox6->setObjectName(QStringLiteral("checkBox6"));
        checkBox6->setGeometry(QRect(40, 100, 111, 41));
        checkBox4 = new QCheckBox(widget);
        checkBox4->setObjectName(QStringLiteral("checkBox4"));
        checkBox4->setGeometry(QRect(40, 20, 111, 41));
        checkBox4->setAutoExclusive(false);
        checkBox5 = new QCheckBox(widget);
        checkBox5->setObjectName(QStringLiteral("checkBox5"));
        checkBox5->setGeometry(QRect(40, 60, 111, 41));
        groupBox = new QGroupBox(QCheckBoxTestClass);
        groupBox->setObjectName(QStringLiteral("groupBox"));
        groupBox->setGeometry(QRect(150, 140, 221, 171));
        checkBox3 = new QCheckBox(groupBox);
        checkBox3->setObjectName(QStringLiteral("checkBox3"));
        checkBox3->setGeometry(QRect(40, 100, 111, 41));
        checkBox1 = new QCheckBox(groupBox);
        checkBox1->setObjectName(QStringLiteral("checkBox1"));
        checkBox1->setGeometry(QRect(40, 20, 111, 41));
        checkBox1->setAutoExclusive(false);
        checkBox2 = new QCheckBox(groupBox);
        checkBox2->setObjectName(QStringLiteral("checkBox2"));
        checkBox2->setGeometry(QRect(40, 60, 111, 41));
        checkBox7 = new QCheckBox(QCheckBoxTestClass);
        checkBox7->setObjectName(QStringLiteral("checkBox7"));
        checkBox7->setGeometry(QRect(180, 370, 101, 31));
        TeseButton = new QPushButton(QCheckBoxTestClass);
        TeseButton->setObjectName(QStringLiteral("TeseButton"));
        TeseButton->setGeometry(QRect(60, 370, 75, 23));

        retranslateUi(QCheckBoxTestClass);

        QMetaObject::connectSlotsByName(QCheckBoxTestClass);
    } // setupUi

    void retranslateUi(QWidget *QCheckBoxTestClass)
    {
        QCheckBoxTestClass->setWindowTitle(QApplication::translate("QCheckBoxTestClass", "QCheckBoxTest", Q_NULLPTR));
        checkBox6->setText(QApplication::translate("QCheckBoxTestClass", "CheckBox6", Q_NULLPTR));
        checkBox4->setText(QApplication::translate("QCheckBoxTestClass", "CheckBox4", Q_NULLPTR));
        checkBox5->setText(QApplication::translate("QCheckBoxTestClass", "CheckBox5", Q_NULLPTR));
        groupBox->setTitle(QApplication::translate("QCheckBoxTestClass", "GroupBox", Q_NULLPTR));
        checkBox3->setText(QApplication::translate("QCheckBoxTestClass", "CheckBox3", Q_NULLPTR));
        checkBox1->setText(QApplication::translate("QCheckBoxTestClass", "CheckBox1", Q_NULLPTR));
        checkBox2->setText(QApplication::translate("QCheckBoxTestClass", "CheckBox2", Q_NULLPTR));
        checkBox7->setText(QApplication::translate("QCheckBoxTestClass", "CheckBox7", Q_NULLPTR));
        TeseButton->setText(QApplication::translate("QCheckBoxTestClass", "Test", Q_NULLPTR));
    } // retranslateUi

};

namespace Ui {
    class QCheckBoxTestClass: public Ui_QCheckBoxTestClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_QCHECKBOXTEST_H

主函数:main.cpp

#include "qcheckboxtest.h"
#include <QtWidgets/QApplication>

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

实现文件:qcheckboxtest.cpp

#include "qcheckboxtest.h"
#include <QDebug>
QCheckBoxTest::QCheckBoxTest(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
//控件设置:
	//设置checkBox的自动排它属性;就是只能选择一个,当然得都打开此属性才行。
	ui.checkBox1->setAutoExclusive(true);
	ui.checkBox2->setAutoExclusive(true);
	ui.checkBox3->setAutoExclusive(true);

//信号与槽绑定
	connect(ui.checkBox7, SIGNAL(clicked(bool)), this, SLOT(Click(bool)));
	connect(ui.checkBox7, SIGNAL(toggled(bool)), this, SLOT(Toggle(bool)));
	connect(ui.TeseButton, SIGNAL(clicked()), this, SLOT(Test()));
}
//只有按下checkBox7才会响应事件
void QCheckBoxTest::Click(bool check)
{
	qDebug() << "Chilck:" << check;
}
//通过按下checkBox7会响应事件,并且button按钮按下也会响应事件
void QCheckBoxTest::Toggle(bool check)
{
	qDebug() << "Toggle:" << check;
}
//对checkBox状态取反,isChecked()默认设置为false
void QCheckBoxTest::Test()
{
	ui.checkBox7->setChecked(!ui.checkBox7->isChecked());
}

ui界面布局样式

在这里插入图片描述

运行结果图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳一航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值