Set the button with the selected state, associate the button to QButtonGroup, and associate QStackedWidget display in the QButtonGroup click event. The effect is as follows: The
code is as follows:
testwgt.h
#ifndef TESTWGT_H
#define TESTWGT_H
#include <QWidget>
#include <QButtonGroup>
#include <QStackedWidget>
class TestWgt : public QWidget
{
Q_OBJECT
public:
explicit TestWgt(QWidget *parent = 0);
~TestWgt();
private:
void initObject();
void initConnect();
private slots:
void slot_BtnTabGroupChanged(int index);
private:
QButtonGroup* m_pTabGroup;
QStackedWidget* m_pStackedWgt;
};
#endif // TESTWGT_H
testwgt.cpp
#include "testwgt.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
TestWgt::TestWgt(QWidget *parent) :
QWidget(parent,Qt::FramelessWindowHint)
{
initObject();
initConnect();
}
TestWgt::~TestWgt()
{
}
void TestWgt::initObject()
{
this->setFixedSize(400,300);
QPushButton* pSetBtn1 = new QPushButton();
pSetBtn1->setFixedSize(100, 30);
pSetBtn1->setFlat(false);
pSetBtn1->setCheckable(true);
pSetBtn1->setChecked(true);
pSetBtn1->setText(QString::fromLocal8Bit("设置1"));
pSetBtn1->setObjectName("tabBtn");
QPushButton* pSetBtn2 = new QPushButton();
pSetBtn2->setFixedSize(100, 30);
pSetBtn2->setFlat(false);
pSetBtn2->setCheckable(true);
pSetBtn2->setText(QString::fromLocal8Bit("设置2"));
pSetBtn2->setObjectName("tabBtn");
QPushButton* pSetBtn3 = new QPushButton();
pSetBtn3->setFixedSize(100, 30);
pSetBtn3->setFlat(false);
pSetBtn3->setCheckable(true);
pSetBtn3->setText(QString::fromLocal8Bit("设置3"));
pSetBtn3->setObjectName("tabBtn");
QHBoxLayout* pTabHlayout = new QHBoxLayout();
pTabHlayout->addWidget(pSetBtn1);
pTabHlayout->addWidget(pSetBtn2);
pTabHlayout->addWidget(pSetBtn3);
m_pTabGroup = new QButtonGroup();
m_pTabGroup->addButton(pSetBtn1, 0);
m_pTabGroup->addButton(pSetBtn2, 1);
m_pTabGroup->addButton(pSetBtn3, 2);
QWidget * pSetWgt1 = new QWidget();
QLabel* pInfoLabe1 = new QLabel();
pInfoLabe1->setFixedSize(80, 60);
pInfoLabe1->setText(QString::fromLocal8Bit("第一页"));
QHBoxLayout* pHlayout1 = new QHBoxLayout(pSetWgt1);
pHlayout1->addWidget(pInfoLabe1);
pHlayout1->setAlignment(pInfoLabe1,Qt::AlignCenter);
QWidget * pSetWgt2 = new QWidget();
QLabel* pInfoLabe2 = new QLabel();
pInfoLabe2->setFixedSize(80, 60);
pInfoLabe2->setText(QString::fromLocal8Bit("第二页"));
QHBoxLayout* pHlayout2 = new QHBoxLayout(pSetWgt2);
pHlayout2->addWidget(pInfoLabe2);
pHlayout2->setAlignment(pInfoLabe2,Qt::AlignCenter);
QWidget * pSetWgt3 = new QWidget();
QLabel* pInfoLabe3 = new QLabel();
pInfoLabe3->setFixedSize(80, 60);
pInfoLabe3->setText(QString::fromLocal8Bit("第三页"));
QHBoxLayout* pHlayout3 = new QHBoxLayout(pSetWgt3);
pHlayout3->addWidget(pInfoLabe3);
pHlayout3->setAlignment(pInfoLabe3,Qt::AlignCenter);
pSetWgt1->setStyleSheet("background-color:red");
pSetWgt2->setStyleSheet("background-color:green");
pSetWgt3->setStyleSheet("background-color:blue");
m_pStackedWgt = new QStackedWidget();
m_pStackedWgt->insertWidget(0, pSetWgt1);
m_pStackedWgt->insertWidget(1, pSetWgt2);
m_pStackedWgt->insertWidget(2, pSetWgt3);
QVBoxLayout* pSetMainVlayout = new QVBoxLayout(this);
pSetMainVlayout->setMargin(0);
pSetMainVlayout->setSpacing(0);
pSetMainVlayout->addLayout(pTabHlayout);
pSetMainVlayout->addWidget(m_pStackedWgt);
this->setStyleSheet(QString("QPushButton:hover#tabBtn{background-color: #3b82ff; outline: none;"
"border: none; color: #c3c6d7; font: 16px;}"
"QPushButton:Checked#tabBtn{background-color: #3b82ff; outline: none;"));
}
void TestWgt::initConnect()
{
connect(m_pTabGroup, SIGNAL(buttonClicked(int)),this, SLOT(slot_BtnTabGroupChanged(int)));
}
void TestWgt::slot_BtnTabGroupChanged(int index)
{
if(!m_pStackedWgt)
{
return;
}
m_pStackedWgt->setCurrentIndex(index);
}
main.cpp
#include <QApplication>
#include "testwgt.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TestWgt w;
w.show();
return a.exec();
}