切换风格的写法:
先看看样式效果:
mian_window.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();void InitMenu();void InitToolBar();
private:void SetStyleSheetToolBar();void InitFile();
private:QTabWidget *toolTablWidget_ {nullptr};
};
#endif // MAINWINDOW_H
cpp 文件
#include "mainwindow.h"#include <QApplication>
#include <QMainWindow>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QWidget>
#include <QLabel>
#include <QStackedWidget>
#include <QScreen>
#include <QGuiApplication>
#include <QMenuBar>
#include "file_widget.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{// 创建中央窗口部件QWidget *centralWidget = new QWidget(this);setCentralWidget(centralWidget);InitToolBar();InitMenu();// 设置布局QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);mainLayout->addWidget(toolTablWidget_);mainLayout->addWidget(new QWidget);mainLayout->addStretch();centralWidget->setLayout(mainLayout);// 设置窗口标题setWindowTitle("默认选项卡的 QMainWindow");
}MainWindow::~MainWindow() {}void MainWindow::InitMenu()
{// 创建菜单栏QMenuBar *menuBar = new QMenuBar(this);setMenuBar(menuBar);// 添加“文件”菜单QMenu *fileMenu = new QMenu("文件", this);menuBar->addMenu(fileMenu);fileMenu->addAction("新建");fileMenu->addAction("打开");fileMenu->addAction("保存");fileMenu->addSeparator();QAction *exitAction = fileMenu->addAction("退出");connect(exitAction, &QAction::triggered, this, &QMainWindow::close);// 添加“视图”菜单QMenu *viewMenu = new QMenu("视图", this);menuBar->addMenu(viewMenu);viewMenu->addAction("放大");viewMenu->addAction("缩小");// 添加“帮助”菜单QMenu *helpMenu = new QMenu("帮助", this);menuBar->addMenu(helpMenu);helpMenu->addAction("关于");helpMenu->addAction("帮助");
}void MainWindow::InitToolBar()
{// 获取屏幕高度QScreen *screen = QGuiApplication::primaryScreen();int screenHeight = screen->size().height();int tabWidgetHeight = screenHeight / 7; // 设置 tabWidget 高度为屏幕高度的八分之一// 创建 QTabWidgettoolTablWidget_ = new QTabWidget(this);toolTablWidget_->setFixedHeight(tabWidgetHeight);SetStyleSheetToolBar();// 添加选项卡到 QTabWidget,并设置文本toolTablWidget_->addTab(new QWidget(), "选项卡 1");toolTablWidget_->addTab(new QWidget(), "选项卡 2");toolTablWidget_->addTab(new QWidget(), "选项卡 3");toolTablWidget_->addTab(new QWidget(), "选项卡 4");toolTablWidget_->addTab(new QWidget(), "选项卡 5");InitFile();
}void MainWindow::SetStyleSheetToolBar()
{QString styleSheet = R"(QMainWindow {background-color: #f0f0f0; /* 设置背景颜色为浅灰色 */
}QTabWidget::pane {border: none;background: transparent;
}QTabBar::tab {background: rgba(255, 255, 255, 180); /* 半透明背景 */width: 120px; /* 设置页签宽度为120px */height: 22px;margin-right: 5px; /* 页签之间的间距 */margin-bottom: 10px; /* 页签与页面之间的距离 */
}QTabBar::tab:selected {background: rgba(255, 255, 200, 0); /* 选中的页签背景为不透明白色 */
}QTabWidget > QWidget > QWidget {border: none;border-right: 0px solid gray; /* 右边框 */border-bottom: 0px solid gray; /* 下边框 */border-radius: 16px 16px 16px 16px; /* 下方圆角 */background: rgba(255, 255, 255, 255); /* 半透明背景 */margin-top: 0px; /* 将页面向上移动1像素,以隐藏面板边框 */
}QGroupBox {border: 1px solid #C0C0C0; /* 边框颜色 */border-radius: 10px; /* 设置圆角为5像素 */background: rgba(255, 255, 255, 180); /* 半透明背景 */padding: 5px; /* 内边距 */margin-bottom: 2px; /* 为了确保标题在外部下方居中 */margin-top: 0px; /* 为了确保标题在外部下方居中 */
}QGroupBox::title {subcontrol-origin: padding;subcontrol-position: bottom center; /* 标题文字在下方居中 */padding: 0 10px;background: transparent;color: black;
})";this->setStyleSheet(styleSheet);
}void MainWindow::InitFile()
{PageFileWidget * pageFile = new PageFileWidget(toolTablWidget_->widget(0));pageFile->Init();
}
下面向日葵所在的分文件:
#ifndef FILE_WIDGET_H
#define FILE_WIDGET_H#include <QWidget>class PageFileWidget : public QWidget {Q_OBJECT
public:explicit PageFileWidget(QWidget *parent = nullptr);void Init();
private:void setupUI();
};#endif // FILE_WIDGET_H#include "file_widget.h"
#include <QGridLayout>
#include <QPushButton>
#include <QIcon>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QComboBox>
#include <QGroupBox>
#include <QToolButton>namespace {
const int TOOLBUTTON_WIDTH = 40;
const int TOOLBUTTON_HEIGHT = 40;
}
PageFileWidget::PageFileWidget(QWidget *parent) : QWidget(parent) {}void PageFileWidget::Init()
{setupUI();
}void PageFileWidget::setupUI() {QHBoxLayout *hbox = new QHBoxLayout(this);QGridLayout *gridLayout = new QGridLayout(this);// 设置边距(上下左右均为5px)gridLayout->setContentsMargins(1,1,1,1);QGridLayout *gridLayoutfile = new QGridLayout(this);gridLayoutfile->setContentsMargins(5, 5, 5, 5);QGroupBox *fileGroup = new QGroupBox("File", this);QGroupBox *fileConstruct = new QGroupBox("construct", this);// QString styleSheet = R"(// )";
// fileGroup->setStyleSheet(styleSheet);
// fileConstruct->setStyleSheet(styleSheet);// 添加按钮和标签,模拟 PowerPoint 界面QToolButton *button1 = new QToolButton(this);button1->setText("Button Text"); // 设置按钮的文字button1->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button1->setIconSize(QSize(TOOLBUTTON_WIDTH, TOOLBUTTON_HEIGHT)); // 设置图标的大小(可选)QToolButton *button2 = new QToolButton(this);button2->setText("Button Text"); // 设置按钮的文字button2->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button2->setIconSize(QSize(TOOLBUTTON_WIDTH, TOOLBUTTON_HEIGHT)); // 设置图标的大小(可选)QToolButton *button3 = new QToolButton(this);button3->setText("Button Text"); // 设置按钮的文字button3->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button3->setIconSize(QSize(TOOLBUTTON_WIDTH, TOOLBUTTON_HEIGHT)); // 设置图标的大小(可选)QToolButton *button4 = new QToolButton(this);button4->setText("Button Text"); // 设置按钮的文字button4->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button4->setIconSize(QSize(TOOLBUTTON_WIDTH, TOOLBUTTON_HEIGHT)); // 设置图标的大小(可选)QToolButton *button5 = new QToolButton(this);button5->setText("Button Text"); // 设置按钮的文字button5->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button5->setIconSize(QSize(TOOLBUTTON_WIDTH, TOOLBUTTON_HEIGHT)); // 设置图标的大小(可选)QToolButton *button6 = new QToolButton(this);button6->setText("Button Text"); // 设置按钮的文字button6->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button6->setIconSize(QSize(TOOLBUTTON_WIDTH, TOOLBUTTON_HEIGHT)); // 设置图标的大小(可选)QToolButton *button7 = new QToolButton(this);button7->setText("Button Text"); // 设置按钮的文字button7->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button7->setIconSize(QSize(32, 32)); // 设置图标的大小(可选)QToolButton *button8 = new QToolButton(this);button8->setText("Button Text"); // 设置按钮的文字button8->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button8->setIconSize(QSize(TOOLBUTTON_WIDTH, TOOLBUTTON_HEIGHT)); // 设置图标的大小(可选)QToolButton *button9 = new QToolButton(this);button9->setText("Button Text"); // 设置按钮的文字button9->setIcon(QIcon(":/icons/1.png")); // 设置按钮的图标button9->setIconSize(QSize(TOOLBUTTON_WIDTH, TOOLBUTTON_HEIGHT)); // 设置图标的大小(可选)gridLayout->addWidget(button1, 0, 0);gridLayout->addWidget(button2, 0, 1);gridLayout->addWidget(button3, 0, 2);gridLayout->addWidget(button4, 0, 3);gridLayoutfile->addWidget(button5, 0, 0);gridLayoutfile->addWidget(button6, 0, 1);gridLayoutfile->addWidget(button7, 1, 0);gridLayoutfile->addWidget(button8, 1, 1);gridLayoutfile->addWidget(button9, 1, 2);// 添加标签用于描述每一行的内容// QLabel *label1 = new QLabel("素材", this);// QLabel *label2 = new QLabel("一键美化", this);// 将标签添加到布局// gridLayout->addWidget(label1, 2, 1, 1, 2, Qt::AlignHCenter);// gridLayoutfile->addWidget(label2, 3, 1, 1, 2, Qt::AlignHCenter);fileGroup->setLayout(gridLayout);fileConstruct->setLayout(gridLayoutfile);hbox->addWidget(fileGroup);hbox->setSpacing(5);hbox->addWidget(fileConstruct);hbox->setSpacing(5);setLayout(hbox);
}