BitWidget,自定义bit控件

 由于QBitArray并不满足我做界面是的需求,所以参照QBitArray简单的写了个控件,如下所示,源码及实例在我上传的资源包中

实例

帮助文档如图所示(部分)

帮助文档(在资源包中)

1.html文档

2.chm文档

源码

头文件

#ifndef BITWIDGET_H
#define BITWIDGET_H

#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
#include <QLabel>
#include <QCheckBox>
#include <QBitArray>
#include <QDebug>

namespace Ui {
class BitWidget;
}
/**
 * @brief 辅助做单个bit显示控件
 */
struct Bit
{
    QLabel* bitLabel=nullptr;///< 界面显示bit文本标签
    QCheckBox* bitCheckBox=nullptr;///< 界面显示bit值复选框
};

class BitWidget : public QWidget
{
    Q_OBJECT

public:
    /**
     * @brief bit控件
     * @param bitSize bit位数
     * @param AlignmentFlag 对齐方式:仅对两个参数有效Qt::AlignHCenter与Qt::AlignVCenter
     * @param parent
     */
    explicit BitWidget(int bitSize=8,Qt::AlignmentFlag AlignmentFlag=Qt::AlignHCenter,QWidget *parent = 0);
    ~BitWidget();
    int setValue(const int &value);///< 设置置
    int getValue();///< 获取值
    bool at(int bitPos) const;///<获取bitPos值
    void clear();///< 将所有bit位置0
    void clearBit(int bitPos);///< 将bitPos值置0
    int count() const;///<返回bit数
    int count(bool on) const;///<返回1的数量
    bool fill(bool value, int size = -1);///<从bit0开始填充,如果size=-1,则填充所有,否则,从0开始填充size数量的value
    bool fill(bool value, int begin, int end);///< 从begin开始填充置end;如果end>begin,则返回false,如果end>bit位总数,则填充置bit位总数
    bool isEmpty() const;///< bit位总数为0,返回false,否则返回true
    void resize(int bitSize);///< 重设bit位总数
    bool setBit(int bitPos);///< 将bitPos置1,成功返回true,否则返回false;
    bool setBit(int bitPos, bool value);///< 将bitPos置value,成功返回true,否则返回false;
    int size() const;///< 等价count()
    bool testBit(int bitPos) const;///< 等价setBit(int bitPos)
    bool toggleBit(int bitPos);///< 翻转bitPos位,成功返回ture,否则返回false

private:
    void Init();
    void UiInit();
private:
    Ui::BitWidget *ui;
    int m_bitSize;///< bit 数量
    Qt::AlignmentFlag m_AlignmentFlag;///< 对齐方式
    QVector<Bit> m_VBit;///< bit控件
    QGridLayout *m_layout=nullptr;///< 界面布局
};

#endif // BITWIDGET_H

源文件

#include "bitwidget.h"
#include "ui_bitwidget.h"
#include "Windows.h"

BitWidget::BitWidget(int bitSize, Qt::AlignmentFlag AlignmentFlag, QWidget *parent) :
    m_bitSize(bitSize),
    m_AlignmentFlag(AlignmentFlag),
    QWidget(parent),
    ui(new Ui::BitWidget)
{
    ui->setupUi(this);
    Init();
}

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

bool BitWidget::at(int bitPos) const
{
    if(bitPos>m_bitSize-1)return false;
    return m_VBit.at(bitPos).bitCheckBox->isChecked();
}

void BitWidget::clear()
{
    for(int i=0;i<m_bitSize;++i)
    {
        m_VBit.at(i).bitCheckBox->setChecked(false);
    }
}

void BitWidget::clearBit(int bitPos)
{
    if(bitPos>m_bitSize-1)return;
    m_VBit.at(bitPos).bitCheckBox->setChecked(false);
}

int BitWidget::count() const
{
    return m_bitSize;
}

int BitWidget::count(bool on) const
{
    int cnt=0;
    for(int i=0;i<m_bitSize;++i)
    {
        if(m_VBit.at(i).bitCheckBox->isChecked()==on)
        {
            cnt++;
        }
    }
    return cnt;
}

bool BitWidget::fill(bool value, int size)
{
    int fillsize=0;
    if(size==-1)
    {
        fillsize=m_bitSize-1;
    }else
    {
        fillsize=size-1;
    }
    if(m_bitSize==0)return false;
    for(int i=0;i<m_bitSize;++i)
    {
        if(i>fillsize)break;
        m_VBit.at(i).bitCheckBox->setChecked(value);
    }
    return true;
}

bool BitWidget::fill(bool value, int begin, int end)
{
    if(begin>end)return false;
    for(int i=begin;i<end+1;++i)
    {
        if(i>m_bitSize-1)break;
        m_VBit.at(i).bitCheckBox->setChecked(value);
    }
    return true;
}

bool BitWidget::isEmpty() const
{
    return m_VBit.isEmpty();
}

void BitWidget::resize(int bitSize)
{
    if(m_VBit.length()>bitSize)
    {
        for(int i = 0;i<m_VBit.length();++i)
        {
            if(i>=bitSize)
            {
                m_VBit.at(i).bitLabel->hide();
                m_VBit.at(i).bitCheckBox->hide();
                m_VBit.at(i).bitCheckBox->setChecked(false);
            }
            else
            {
                m_VBit.at(i).bitLabel->show();
                m_VBit.at(i).bitCheckBox->show();
            }
        }
    }
    else if(m_VBit.length()<=bitSize)
    {
        switch (m_AlignmentFlag) {
        case Qt::AlignHCenter:
        {
            for(int col=m_VBit.length();col<bitSize;++col)
            {
                Bit bit;
                bit.bitLabel=new QLabel(this);
                bit.bitCheckBox=new QCheckBox(this);
                bit.bitLabel->setText(QString("%1").arg(col));
                bit.bitLabel->setAlignment(Qt::AlignBottom);
                m_layout->addWidget(bit.bitLabel,0,col,1,1);
                m_layout->addWidget(bit.bitCheckBox,1,col,1,1);
                m_layout->setSpacing(1);
                this->setLayout(m_layout);
                m_VBit.append(bit);
            }
        }
            break;
        case Qt::AlignVCenter:
        {
            for(int row=m_VBit.length();row<bitSize;++row)
            {
                Bit bit;
                bit.bitLabel=new QLabel(this);
                bit.bitCheckBox=new QCheckBox(this);
                bit.bitLabel->setText(QString("%1").arg(row));
                bit.bitLabel->setAlignment(Qt::AlignRight);
                m_layout->addWidget(bit.bitLabel,row,0,1,1);
                m_layout->addWidget(bit.bitCheckBox,row,1,1,1);
                m_layout->setSpacing(1);
                this->setLayout(m_layout);
                m_VBit.append(bit);
            }
        }
            break;
        default:
            break;
        }
        for(int i = 0;i<m_VBit.length();++i)
        {
            m_VBit.at(i).bitLabel->show();
            m_VBit.at(i).bitCheckBox->show();
        }
    }
    m_bitSize=bitSize;
}

bool BitWidget::setBit(int bitPos)
{
    if(bitPos>m_bitSize-1)return false;
    m_VBit.at(bitPos).bitCheckBox->setChecked(true);
    return true;
}

bool BitWidget::setBit(int bitPos, bool value)
{
    if(bitPos>m_bitSize-1)return false;
    m_VBit.at(bitPos).bitCheckBox->setChecked(value);
    return true;
}

int BitWidget::size() const
{
    return count();
}

bool BitWidget::testBit(int bitPos) const
{
    if(bitPos>m_bitSize-1)return false;
    m_VBit.at(bitPos).bitCheckBox->setChecked(true);
    return true;
}

bool BitWidget::toggleBit(int bitPos)
{
    if(bitPos>m_bitSize-1)return false;
    auto val=m_VBit.at(bitPos).bitCheckBox->isChecked();
    val=!val;
    m_VBit.at(bitPos).bitCheckBox->setChecked(val);
    return true;
}

void BitWidget::Init()
{
    UiInit();
}

void BitWidget::UiInit()
{
    if(m_layout==nullptr)
    {
        m_layout=new QGridLayout(this);
    }
    switch (m_AlignmentFlag) {
    case Qt::AlignHCenter:
    {
        for(int col=0;col<m_bitSize;++col)
        {
            Bit bit;
            bit.bitLabel=new QLabel(this);
            bit.bitCheckBox=new QCheckBox(this);
            bit.bitLabel->setText(QString("%1").arg(col));
            bit.bitLabel->setAlignment(Qt::AlignBottom);
            m_layout->addWidget(bit.bitLabel,0,col,1,1);
            m_layout->addWidget(bit.bitCheckBox,1,col,1,1);
            m_layout->setSpacing(1);
            this->setLayout(m_layout);
            m_VBit.append(bit);
        }
    }
        break;
    case Qt::AlignVCenter:
    {
        for(int row=0;row<m_bitSize;++row)
        {
            Bit bit;
            bit.bitLabel=new QLabel(this);
            bit.bitCheckBox=new QCheckBox(this);
            bit.bitLabel->setText(QString("%1").arg(row));
            bit.bitLabel->setAlignment(Qt::AlignRight);
            m_layout->addWidget(bit.bitLabel,row,0,1,1);
            m_layout->addWidget(bit.bitCheckBox,row,1,1,1);
            m_layout->setSpacing(1);
            this->setLayout(m_layout);
            m_VBit.append(bit);
        }
    }
        break;
    default:
        break;
    }
}


int BitWidget::setValue(const int &value)
{
    int bitPos=-1;
    auto temp=value;
    for(int i=0;i<m_bitSize;++i)
    {
        m_VBit.at(i).bitCheckBox->setChecked(false);
    }
    while(temp)
    {
        bitPos++;
        if(bitPos>=m_bitSize)break;
        if(temp&0x01)
        {
            m_VBit.at(bitPos).bitCheckBox->setChecked(true);
        }
        temp >>= 1;
    }
    return bitPos;
}

int BitWidget::getValue()
{
    int val=0;
    for(int i=0;i<m_bitSize;++i)
    {
        auto temp=m_VBit.at(i).bitCheckBox->isChecked()?1:0;
        val|=(temp<<i);
    }
    return val;
}

 实例代码

 example头文件

#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <QWidget>
#include "bitwidget.h"

namespace Ui {
class Example;
}

class Example : public QWidget
{
    Q_OBJECT

public:
    explicit Example(QWidget *parent = 0);
    ~Example();

private slots:

    void on_pushButtonat_clicked();

    void on_pushButtonclear_clicked();

    void on_pushButtonclearBit_clicked();

    void on_pushButtoncount_clicked();

    void on_pushButtoncount_on_clicked();

    void on_pushButtonfill_clicked();

    void on_pushButtonfill_begin_end_clicked();

    void on_pushButtonisEmpty_clicked();

    void on_pushButtonresize_clicked();

    void on_pushButtonsetBit_clicked();

    void on_pushButtonsetBit_value_clicked();

    void on_pushButtonsize_clicked();

    void on_pushButtontestBit_clicked();

    void on_pushButtontoggleBit_clicked();

    void on_pushButtonsetValue_clicked();

    void on_pushButtongetValue_clicked();

private:
    Ui::Example *ui;
    BitWidget *m_bitWidget=nullptr;
};

#endif // EXAMPLE_H

example源文件

#include "example.h"
#include "ui_example.h"

Example::Example(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Example)
{
    ui->setupUi(this);
    m_bitWidget=new BitWidget(8,Qt::AlignHCenter,this);
    ui->gridLayoutBitWidget->addWidget(m_bitWidget,0,0,1,1);
}

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

void Example::on_pushButtonat_clicked()
{
    ui->labelat->setText(m_bitWidget->at(ui->spinBoxat->value())?"1":"0");
}

void Example::on_pushButtonclear_clicked()
{
    m_bitWidget->clear();
}

void Example::on_pushButtonclearBit_clicked()
{
    m_bitWidget->clearBit(ui->spinBoxclearBit->value());
}

void Example::on_pushButtoncount_clicked()
{
    ui->labelcount->setNum(m_bitWidget->count());
}

void Example::on_pushButtoncount_on_clicked()
{
    ui->labelcount_on->setNum(m_bitWidget->count(ui->checkBoxcount_on->isChecked()));
}

void Example::on_pushButtonfill_clicked()
{
    m_bitWidget->fill(ui->checkBoxvalue->isChecked(),ui->spinBoxfill->value());
}

void Example::on_pushButtonfill_begin_end_clicked()
{
    m_bitWidget->fill(ui->checkBoxvalue_2->isChecked(),ui->spinBoxfillbegin->value(),ui->spinBoxfillend->value());
}

void Example::on_pushButtonisEmpty_clicked()
{
    ui->labelisEmpty->setText(m_bitWidget->isEmpty()?"ture":"false");
}

void Example::on_pushButtonresize_clicked()
{
    m_bitWidget->resize(ui->spinBoxresize->value());
}

void Example::on_pushButtonsetBit_clicked()
{
    m_bitWidget->setBit(ui->spinBoxsetBit->value());
}

void Example::on_pushButtonsetBit_value_clicked()
{
    m_bitWidget->setBit(ui->spinBoxsetBit_value->value(),ui->checkBoxsetBit_value->isChecked());
}

void Example::on_pushButtonsize_clicked()
{
    ui->labelsize->setNum(m_bitWidget->size());
}

void Example::on_pushButtontestBit_clicked()
{
    m_bitWidget->setBit(ui->spinBoxtestBit->value());
}

void Example::on_pushButtontoggleBit_clicked()
{
    m_bitWidget->toggleBit(ui->spinBoxtoggleBit->value());
}

void Example::on_pushButtonsetValue_clicked()
{
   auto ret= m_bitWidget->setValue(ui->spinBoxsetValue->value());
   ui->labelsetValue->setNum(ret);
}

void Example::on_pushButtongetValue_clicked()
{
    ui->labelgetValue->setNum(m_bitWidget->getValue());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

春意盎然的三月

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

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

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

打赏作者

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

抵扣说明:

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

余额充值