#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QStackedWidget>
#include <QTabBar>
#include <QLabel>
#include <QDebug>
#include <QString>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
QStackedWidget m_stackedWidget;
QTextEdit m_TextEdit;
QLabel m_lab2;
QLabel m_lab3;
QPushButton m_btn;
QTabBar m_tabBar;
protected slots:
void currentRowChangeSlot(int row);
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H
#include "Widget.h"
Widget::Widget(QWidget *parent) : QWidget(parent)
{
QVBoxLayout* vLayout = new QVBoxLayout();
m_TextEdit.append("文本框");
m_lab2.setText("标签2");
m_lab3.setText("标签3");
m_btn.setText("按钮");
m_stackedWidget.addWidget(&m_TextEdit);
m_stackedWidget.addWidget(&m_lab2);
m_stackedWidget.addWidget(&m_lab3);
m_stackedWidget.addWidget(&m_btn);
m_stackedWidget.setCurrentIndex(0);
m_tabBar.addTab("文本框");
m_tabBar.addTab("标签2");
m_tabBar.addTab("标签3");
m_tabBar.addTab("按钮");
m_tabBar.setShape(QTabBar::RoundedNorth);
m_tabBar.setCurrentIndex(0);
vLayout->addWidget(&m_tabBar);
vLayout->addWidget(&m_stackedWidget);
connect(&m_tabBar, SIGNAL(currentChanged(int)), this, SLOT(currentRowChangeSlot(int)));
vLayout->setStretchFactor(&m_tabBar, 1);
vLayout->setStretchFactor(&m_stackedWidget, 3);
setLayout(vLayout);
}
void Widget::currentRowChangeSlot(int row)
{
m_stackedWidget.setCurrentIndex(row);
}
Widget::~Widget()
{
}
运行效果图: