Qt自定义控件 - CheckBox自定义控件

本文目录

样板图


功能描述:
1、可以单个勾选、也可以按钮一次性全部勾选和清除。
2、能够响应勾选和清除的状态,样例代码中是qDebug输出。

代码详解

checkwidget.h头文件

// checkwidget.h
// @Author : fy

#ifndef CHECKWIDGET_H
#define CHECKWIDGET_H

#include <QWidget>

#include <QCheckBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QGroupBox>
#include <QVector>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class CheckWidget : public QWidget
{
    Q_OBJECT

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


private:
    void initUI();

private:
    Ui::Widget *ui;

    QVector<QCheckBox *> m_vCheckBox;       // 存放选择按钮组
    QVector<QVBoxLayout *> m_vVLayout;      // 存放多列CheckBox

    QHBoxLayout *totalCheckLayout;      // 存放整体checkBox组

    QPushButton *m_selectAll;   // 选择全部
    QPushButton *m_clearAll;    // 清理全部

    QVBoxLayout *vwidget;   // 整体布局
    QVBoxLayout *totalwidget;   // 整体布局框架
    QHBoxLayout *htitle;    // 顶部布局
    QVBoxLayout *vbox;      // 下部布局

    QGroupBox *g_totalbox;    // 整体框架
    QGroupBox *g_checkbox;    // 选择框框架


private slots:
    void cbChanged(int state);
    void selectButtonPress();   // 当选择全部时
    void clearButtonPress();    // 当清理全部时
};
#endif // CHECKWIDGET_H

checkwidget.cpp文件

// checkwidget.h
// @Author : fy

#include "checkwidget.h"
#include "ui_widget.h"

#include <QVBoxLayout>
#include <QDebug>


CheckWidget::CheckWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    initUI();

}

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

void CheckWidget::initUI()
{
    this->setWindowTitle("QCheckBox 示例");
    setGeometry(100,100,300,200);

    // 装填
    for(int i = 0 ;i<10;i++)
    {
        m_vCheckBox.append(new QCheckBox(QString("选项")+QString::number(i),this));
    }

    // 两个Group组件
    this->g_totalbox = new QGroupBox();
    this->g_checkbox = new QGroupBox();

    g_totalbox->setTitle("选择组件");
    g_checkbox->setTitle("组件");


    this->vwidget = new QVBoxLayout();              // 一个布局整体
    this->totalwidget = new QVBoxLayout();          // 一个存放整体QGroupBox布局

    this->htitle = new QHBoxLayout();               // 存放按钮组水平布局
    htitle->addStretch(0);                          // 伸缩为0
//    htitle->setDirection(QBoxLayout::RightToLeft);
    this->m_selectAll = new QPushButton("选择全部");
    this->m_clearAll = new QPushButton("清理全部");
    this->m_selectAll->setFixedSize(100,50);
    this->m_clearAll->setFixedSize(100,50);
    htitle->addWidget(m_selectAll);
    htitle->addWidget(m_clearAll);


    // checkbox 布局分布设置
    vbox = nullptr;
    for(int count =0, i=0;i<m_vCheckBox.size();i++)
    {
        if(i%2 == 0) { vbox = new QVBoxLayout(); }

        vbox->addWidget(m_vCheckBox.at(i));

        if(i%2 == 0 && !(vbox->isEmpty()))
        {
            count++;
            m_vVLayout.append(vbox);
        }
    }


    totalCheckLayout = new QHBoxLayout;
    // 存放多个垂直CheckBox组
    for(int i=0;i<m_vVLayout.size();i++)
    {
       totalCheckLayout->addLayout(m_vVLayout.at(i));

    }
    g_checkbox->setLayout(totalCheckLayout);

    // 顶部和checkBox组,存放一个布局中
    vwidget->addLayout(htitle);
    vwidget->addWidget(g_checkbox);

    // 整体布局存放在一个GroupBox中,然后存放在一个布局中
    g_totalbox->setLayout(vwidget);
    totalwidget->addWidget(g_totalbox);

    // 设置界面
    setLayout(totalwidget);


    // 每一个CheckBox都能选择
    for(int i=0;i<m_vCheckBox.size();i++)
    {
        connect(m_vCheckBox.at(i),&QCheckBox::stateChanged,this,&CheckWidget::cbChanged);
    }

    connect(m_selectAll,&QPushButton::clicked,this,&CheckWidget::selectButtonPress);
    connect(m_clearAll,&QPushButton::clicked,this,&CheckWidget::clearButtonPress);

}

void CheckWidget::cbChanged(int state)
{
    QCheckBox *cb=qobject_cast<QCheckBox *>(sender());

    if(state == Qt::Checked)
    {
        qDebug()<<cb->text()<<"被选中";
    }
    if(state == Qt::Unchecked)
    {
        qDebug()<<cb->text()<<"被取消选中";
    }
}

void CheckWidget::selectButtonPress()
{
    for(int i=0;i<this->m_vCheckBox.size();i++)
    {
        this->m_vCheckBox.at(i)->setChecked(true);
    }
}

void CheckWidget::clearButtonPress()
{
    for(int i=0;i<this->m_vCheckBox.size();i++)
    {
        this->m_vCheckBox.at(i)->setChecked(false);
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QTableView是一个非常灵活的控件,可以很容易地通过自定义来添加图片、复选框、下拉框、按钮、滑条、微调框和日历等控件。 首先,我们可以通过自定义QStandardItemModel来向QTableView中添加图片。可以在数据模型中使用Qt::DecorationRole添加图片数据,并且可以通过自定义代理来实现不同类型的图片展示。 其次,添加复选框可以使用自定义代理来实现。可以通过重载QItemDelegate的paint和editorEvent方法来实现在表格中显示复选框,并且可以通过信号和槽来处理复选框的状态变化。 要在QTableView中使用下拉框,可以通过自定义QComboBoxDelegate来实现下拉框的展示,可以通过paint方法在表格中显示下拉框,同时需要重载editorEvent方法来处理下拉框的交互事件。 添加按钮可以通过自定义QButtonDelegate来实现,通过paint方法在表格中显示按钮,同时需要重载editorEvent方法来处理按钮的交互事件。 要添加滑条,可以通过自定义QSliderDelegate来实现。在表格中通过paint方法显示滑条,并通过editorEvent处理滑条的交互事件。 为了在QTableView中添加微调框,可以通过自定义QSpinBoxDelegate来实现。通过paint方法在表格中显示微调框,并需要通过editorEvent处理微调框的交互事件。 最后,要添加日历控件,可以通过自定义QCalendarDelegate来实现。通过paint方法在表格中显示日历控件,并且通过editorEvent来处理日历的交互事件。 总的来说,通过自定义代理类和数据模型,我们可以方便地向QTableView中添加图片、复选框、下拉框、按钮、滑条、微调框和日历等控件,实现丰富多彩的表格展示效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值