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.xdnf.cn/news/1486299.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

SQL123 SQL类别高难度试卷得分的截断平均值

题目 自测代码 drop table if exists examination_info; CREATE TABLE examination_info (id int PRIMARY KEY AUTO_INCREMENT COMMENT 自增ID,exam_id int UNIQUE NOT NULL COMMENT 试卷ID,tag varchar(32) COMMENT 类别标签,difficulty varchar(8) COMMENT 难度,duration i…

机器学习驱动的智能化电池管理技术与应用

在人工智能与电池管理技术融合的背景下&#xff0c;电池科技的研究和应用正迅速发展&#xff0c;创新解决方案层出不穷。从电池性能的精确评估到复杂电池系统的智能监控&#xff0c;从数据驱动的故障诊断到电池寿命的预测优化&#xff0c;人工智能技术正以其强大的数据处理能力…

数据结构之树的存储结构详解与示例(C/C++)

文章目录 树的存储结构1. 顺序存储结构2. 链式存储结构结论 树&#xff08;Tree&#xff09;是一种非常常见的数据结构&#xff0c;它模拟了一种层级或分支结构。树由节点&#xff08;或称为顶点&#xff09;组成&#xff0c;每个节点包含一个值&#xff0c;并且可能有多个子节…

《500 Lines or Less》(5)异步爬虫

https://aosabook.org/en/500L/a-web-crawler-with-asyncio-coroutines.html ——A. Jesse Jiryu Davis and Guido van Rossum 介绍 网络程序消耗的不是计算资源&#xff0c;而是打开许多缓慢的连接&#xff0c;解决此问题的现代方法是异步IO。 本章介绍一个简单的网络爬虫&a…

使用Python和Pandas导出SQLite数据到Excel的小工具

在数据处理和导出的日常工作中&#xff0c;有时我们需要将SQLite数据库中的数据导出到Excel文件以便进一步分析或分享。本文将介绍如何使用Python的wxPython、Pandas和SQLite3库创建一个小工具&#xff0c;实现从SQLite数据库中提取数据并将其导出到Excel文件的功能。 C:\pytho…

5.Fabric的共识机制

在Fabric中,有以下3中典型共识机制。 Solo共识 solo共识机制只能用于单节点模式,即只能有一个Orderer节点,因此,其共识过程很简单,每接收到一个交易信息,就在共识模块的控制下产生区块并广播给节点存储到账本中。 Solo 模式下的共识只适用于一个Orderer节点,所以可以在…

汉明权重(Hamming Weight)(统计数据中1的个数)VP-SWAR算法

汉明权重&#xff08;Hamming Weight&#xff09;&#xff08;统计数据中1的个数&#xff09;VP-SWAR算法 定义 汉明重量是一串符号中非零符号的个数。它等于同样长度的全零符号串的汉明距离(在信息论中&#xff0c;两个等长字符串之间的汉明距离等于两个字符串对应位置的不同…

无线麦克风推荐哪些品牌,领夹麦克风哪个品牌好,无线麦克风推荐

​作为消费类电子产品&#xff0c;麦克风随着市场需求和技术进步&#xff0c;每年都有新产品系列涌现&#xff0c;特别是领夹麦克风&#xff0c;近年来经历了显著的市场变革和技术突破。从早期的新闻采访、节目录制和影视后期录音中常用的无线小蜜蜂话筒&#xff0c;到如今在网…

【保姆级讲解C语言中的运算符的优先级!】

&#x1f3a5;博主&#xff1a;程序员不想YY啊 &#x1f4ab;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家 &#x1f917;点赞&#x1f388;收藏⭐再看&#x1f4ab;养成习惯 ✨希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出…

创建最佳实践创建 XML 站点地图--SEO

您是否正在努力让您的网站被搜索引擎索引&#xff1f;您想提高您网站的搜索引擎知名度吗&#xff1f;如果是&#xff0c;您可能会错过 XML 站点地图的重要性。XML 站点地图在改善您网站的 SEO 方面发挥着至关重要的作用。‍ XML 站点地图是您网站结构的蓝图&#xff0c;可帮助…

RH436 Managing LVM Shared Volume Groups

RH436 Managing LVM Shared Volume Groups 1. 启动lab环境2. 准备lvm卷组3. 创建逻辑卷4. 配置集群资源启动顺序5. 确认各节点lvs正常6. LVM-HA和LVM-Share使用场景 1. 启动lab环境 [studentworkstation ~]$ lab start lvm-shared2. 准备lvm卷组 所有节点安装依赖包 yum ins…

react中组件间的通信

一、父传子 1.代码展示 import React, { useState } from react;function SonPage(props){ // 子组件const {msg} propsreturn (<div>我是子组件 {msg}</div>) }function App() { // 父组件const [msgText,setMsgText] useState(父传子)return (<div classN…

掌握VR全景技术,需要具备哪些条件?

VR全景技术自从进入市场以来&#xff0c;就在各个行业领域尝试落地运用&#xff0c;包括但不限于广告宣传、学校教育、医疗、工业、农业等领域。随着5G 技术的不断普及&#xff0c;VR全景技术也逐渐被应用到日常生活中的各个方面&#xff0c;从地产中介到车企销售&#xff0c;从…

Electron的入门介绍与使用(1)共30节

Electron是一个使用 JavaScript、HTML 和 CSS 构建桌面应用程序的框架。 嵌入 Chromium 和 Node.js 到 二进制的 Electron 允许您保持一个 JavaScript 代码代码库并创建 在Windows上运行的跨平台应用 macOS和Linux——不需要本地开发 经验。 入门指南​ Electron 是网页应用 …

【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第三篇 嵌入式Linux驱动开发篇-第四十章 Linux用户层和内核层

i.MX8MM处理器采用了先进的14LPCFinFET工艺&#xff0c;提供更快的速度和更高的电源效率;四核Cortex-A53&#xff0c;单核Cortex-M4&#xff0c;多达五个内核 &#xff0c;主频高达1.8GHz&#xff0c;2G DDR4内存、8G EMMC存储。千兆工业级以太网、MIPI-DSI、USB HOST、WIFI/BT…

力扣第二十五题——K个一组反转链表

内容介绍 给你链表的头节点 head &#xff0c;每 k 个节点一组进行翻转&#xff0c;请你返回修改后的链表。 k 是一个正整数&#xff0c;它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍&#xff0c;那么请将最后剩余的节点保持原有顺序。 你不能只是单纯的改变节点内…

Vscode离线下载对应版本的ms-python.vsix

一、查看vscode的版本号和发行时间 vscode界面中Help-About查看版本号和发行时间&#xff0c;ms-python的发行时间需要和这个时间相近&#xff1a; 二、在github仓库中查看ms-python有什么版本&#xff0c;以及发行时间 github仓库路径 https://github.com/microsoft/vsco…

力扣题库合集(2):动态规划(1)

本文将持续更新~~ hello hello~ &#xff0c;这里是绝命Coding——老白~&#x1f496;&#x1f496; &#xff0c;欢迎大家点赞&#x1f973;&#x1f973;关注&#x1f4a5;&#x1f4a5;收藏&#x1f339;&#x1f339;&#x1f339; &#x1f4a5;个人主页&#xff1a;绝命C…

qt中charts图表的使用方法

折线图 #include "widget.h" #include "ui_widget.h" #include <QtCharts/QChart> #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> #include<QVBoxLayout>Widget::Widget(QWidget *parent): QWidget(parent), …

Python解释器:CPython 解释器

一、什么是python解释器 Python解释器是一种用于执行Python代码的程序。 它将Python源代码转换为机器语言或字节码&#xff0c;从而使计算机能够执行。 1.1 Python解释器分类 1、CPython CPython 是 Python 的主要实现&#xff0c;由 C 语言编写。大多数用户在日常开发中使…