当前位置: 首页 > news >正文

QT写一个mainWindow

切换风格的写法:

先看看样式效果:

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);
}

http://www.lryc.cn/news/405599.html

相关文章:

  • Java查找算法练习(2024.7.23)
  • 洗地机哪个牌子好?四款口碑最好的洗地机排名推荐
  • 如何提升短视频的曝光量和获客效能?云微客来解决
  • SpringBoot开发中如何缓存数据, 减少数据库的访问频率?
  • PostgreSQL如何在windows/linux开启归档
  • 【启明智显分享】基于国产Model3芯片的7寸触摸屏助力智慧医疗,电子床头屏提升护理交互
  • 从理论到实践:如何用 TDengine 打造完美数据模型​
  • 可以免费合并pdf的软件 合并pdf文件的软件免费 合并pdf的软件免费
  • 【排序 滑动窗口 】1498. 满足条件的子序列数目
  • RabbitMQ普通集群搭建指南
  • AGV平面坐标系变换公式及实例
  • es切片和集群
  • IEEE官方列表会议 | 第三届能源与环境工程国际会议(CFEEE 2024)
  • 深度学习中的正则化技术 - Dropout篇
  • 《昇思 25 天学习打卡营第 18 天 | 扩散模型(Diffusion Models) 》
  • 【Django+Vue3 线上教育平台项目实战】Elasticsearch实战指南:从基础到构建课程搜索与数据同步接口
  • libtins初探-抓包嗅探
  • 大语言模型-Bert-Bidirectional Encoder Representation from Transformers
  • bug诞生记——动态库加载错乱导致程序执行异常
  • Matlab演示三维坐标系旋转
  • redis的持久化机制以及集群模式
  • 【论文解读】大模型算法发展
  • WebApi配置Swagger、Serilog、NewtonsoftJson、Sqlsugar、依赖注入框架Autofac、MD5加密
  • 【ffmpeg命令基础】视频选项讲解
  • 使用uniapp开发小程序(基础篇)
  • vue3【详解】组合式函数
  • 微服务实战系列之玩转Docker(六)
  • Python题解Leetcode Hot100之动态规划
  • 你了解GD32 MCU上下电要求吗
  • 二、【Python】入门 - 【PyCharm】安装教程