效果图如下:
下面是详细代码实现:
#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()
{
}