层叠样式的按钮组实现

效果图如下:
在这里插入图片描述
下面是详细代码实现:

#ifndef STACKEDBUTTON_H
#define STACKEDBUTTON_H

#include <atomic>
#include <QWidget>
#include <QPushButton>
#include <QButtonGroup>

enum class  LayoutType
{
    UnKnow=0,
    HBoxLayout=1,
    VBoxLayout=2
};

class StackedButton : public QWidget
{
    Q_OBJECT
private:
    int btnSpace;    
    Qt::Alignment align;
    LayoutType layoutType;
    std::atomic_int btnWidth;
    std::atomic_int btnHeight;
    QButtonGroup* btnGroup;
public:
    explicit StackedButton(QWidget *parent = nullptr);
    ~StackedButton();
public:
    void SetBtnStackSpace(int);
    void SetLayoutType(LayoutType);
    void AddButton(QString,QString);
    void AddButton(QString,QString,int,int);
private:
    void InitBtnAddInsert(QPushButton*);
signals:
    void btnClicked(QString);
};
#endif // STACKEDBUTTON_H
#include "StackedButton.h"

constexpr int maginSpace=5;

StackedButton::StackedButton(QWidget *parent) :
    QWidget(parent),btnSpace(0),btnGroup(Q_NULLPTR)
{
    btnWidth=maginSpace;
    btnHeight=maginSpace;
    layoutType=LayoutType::UnKnow;
    btnGroup=new QButtonGroup(this);
}

StackedButton::~StackedButton()
{
    if(btnGroup){
        foreach(QAbstractButton *btn,btnGroup->buttons()){
            if(btn){
                btn->deleteLater();
                btn=Q_NULLPTR;
            }
        }
        btnGroup->deleteLater();
        btnGroup=Q_NULLPTR;
    }
}

void StackedButton::SetLayoutType(LayoutType type)
{
    layoutType=type;
    switch (layoutType) {
    case LayoutType::HBoxLayout:
        if(btnHeight>maginSpace){
            btnHeight=maginSpace;
        }
        break;
    case LayoutType::VBoxLayout:
        if(btnWidth>maginSpace){
            btnWidth=maginSpace;
        }
        break;
    default:
        break;
    }
}

void StackedButton::SetBtnStackSpace(int space)
{
    btnSpace=space;
    switch (layoutType) {
    case LayoutType::HBoxLayout:
        btnWidth+=space;
        break;
    case LayoutType::VBoxLayout:
        btnHeight+=space;
        break;
    default:
        btnWidth+=space;
        btnHeight+=space;
        break;
    }
}

void StackedButton::InitBtnAddInsert(QPushButton *btn)
{
    if(btnGroup&&btn){
        switch (layoutType) {
        case LayoutType::HBoxLayout:
            btn->setGeometry(btnWidth-btnSpace,btnHeight,btn->width(),btn->height());
            btnWidth+=(btn->width()-btnSpace);
            break;
        case LayoutType::VBoxLayout:
            btn->setGeometry(btnWidth,btnHeight-btnSpace,btn->width(),btn->height());
            btnHeight+=(btn->height()-btnSpace);
            break;
        default:
            break;
        }
        btn->setCheckable(true);
        btnGroup->addButton(btn);
        connect(btn,&QPushButton::clicked,this,[=](bool){
            QPushButton* sendBtn=static_cast<QPushButton*>(sender());
            foreach(QAbstractButton *btn,btnGroup->buttons()){
                if(btn){
                    btn->raise();
                }
            }
            sendBtn->raise();
            emit btnClicked(sendBtn->text());
        });
    }
}

void StackedButton::AddButton(QString text,QString objName)
{
    QPushButton* btn=new QPushButton(this);
    btn->setText(text);
    btn->setObjectName(objName);
    InitBtnAddInsert(btn);
}

void StackedButton::AddButton(QString text,QString objName,int width,int height)
{
    QPushButton* btn=new QPushButton(this);
    btn->setText(text);
    btn->setObjectName(objName);
    btn->setFixedSize(width,height);
    InitBtnAddInsert(btn);
}

使用:

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setMinimumSize(400,100);
    stackBtn=new StackedButton;

    stackBtn->SetBtnStackSpace(15);
    stackBtn->SetLayoutType(LayoutType::HBoxLayout);

    stackBtn->AddButton(tr("Btn1"),tr("Button"));
    stackBtn->AddButton(tr("Btn2"),tr("Button"));
    stackBtn->AddButton(tr("Btn2"),tr("Button"));
    stackBtn->AddButton(tr("Btn2"),tr("Button"));

    this->setStyleSheet("QPushButton#Button{border-width: 0 22px 0 4px;border-image: url(://tabBtn-n.png) 0 22 0 4;}"
                        "QPushButton#Button:hover{border-width: 0 22px 0 4px;border-image: url(://tabBtn-h.png) 0 22 0 4;}"
                        "QPushButton#Button:checked{border-width: 0 22px 0 4px;border-image: url(://tabBtn-h.png) 0 22 0 4;}");

    connect(stackBtn,&StackedButton::btnClicked,this,[=](QString text){});

    mainLayout=new QHBoxLayout;
    mainLayout->setMargin(10);
    mainLayout->addWidget(stackBtn);
    setLayout(mainLayout);
}

Widget::~Widget()
{
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值