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

Qt Frameless Widget跨平台无边框窗口

Qt开发的窗口程序,它的标题栏样式是无法修改的,这个是系统来控制,程序可以调整标题,图标等,但是各个系统可能表现不一致,比如说标题,window10下在标题栏最左边,而UOS则在中间,为了保持程序在各个平台下保持一致,无边框窗口是有必要的。

无边框窗口总体思路就是处理鼠标事件,比如窗口拖动和窗口缩放,还需要处理鼠标样式的改变等,qt的MousePressEvent和MouseReleaseEvent等事件就是用来处理这个,代码中FramelessHelper来自于qtcanpool开源项目,效果如下

pro文件:

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \framelesshelper.cpp \main.cpp \widget.cppHEADERS += \framelesshelper.h \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetwin32 {# for FramelessHelperLIBS += -lUser32lessThan(QT_MAJOR_VERSION, 6): DEFINES += FRAMELESS_USE_NATIVE
}

main.cpp

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);// QGuiApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
#endifQCoreApplication::setAttribute(Qt::AA_UseOpenGLES);QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include "framelesshelper.h"QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QTextEdit>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);QTextEdit *edit = new QTextEdit(this);ui->verticalLayout->addWidget(edit);FramelessHelper *helper = new FramelessHelper(this);helper->addWidget(this);
}Widget::~Widget()
{delete ui;
}

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>Widget</class><widget class="QWidget" name="Widget"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="minimumSize"><size><width>400</width><height>600</height></size></property><property name="windowTitle"><string>Widget</string></property><layout class="QHBoxLayout" name="horizontalLayout"><property name="leftMargin"><number>0</number></property><property name="topMargin"><number>0</number></property><property name="rightMargin"><number>0</number></property><property name="bottomMargin"><number>0</number></property><item><layout class="QVBoxLayout" name="verticalLayout"><property name="spacing"><number>0</number></property><item><layout class="QHBoxLayout" name="horizontalLayout_2"><item><widget class="QLabel" name="label"><property name="text"><string>我是一个无边框窗口 哈哈哈</string></property></widget></item><item><spacer name="horizontalSpacer"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item><widget class="QPushButton" name="pushButton_3"><property name="text"><string>最小化</string></property></widget></item><item><widget class="QPushButton" name="pushButton_2"><property name="text"><string>最大化</string></property></widget></item><item><widget class="QPushButton" name="pushButton"><property name="text"><string>关闭</string></property></widget></item></layout></item></layout></item></layout></widget><resources/><connections/>
</ui>

工程源码,欢迎star

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

相关文章:

  • 机器学习-KNN​​
  • 云平台托管集群:EKS、GKE、AKS 深度解析与选型指南-第四章
  • iT 运维: WindoWs CMD 命令表
  • Flutter开发 Image组件使用示例
  • <form> + <iframe> 方式下载大文件的机制
  • 基于Github Pages搭建个人博客站点:hexo环境搭建、本地预览与发布
  • 当前就业形势下,软件测试工程师职业发展与自我提升的必要性
  • AI 软件工程开发 AI 算法 架构与业务
  • [FBCTF2019]RCEService
  • Kafka-exporter采集参数调整方案
  • android NDK 报错日志解读和还原报错方法名
  • 数语科技登陆华为云商店,助力企业释放数据潜能
  • FPGA学习笔记——VGA彩条显示
  • 【软考系统架构设计师备考笔记4】 - 英语语法一篇通
  • 机器人定位装配的精度革命:迁移科技如何重塑工业生产价值
  • Spring Boot 参数校验全指南
  • 应急响应linux
  • vue3+element-plus,el-popover实现筛选弹窗的方法
  • ACL 2025 Oral|Evaluation Agent:面向视觉生成模型的高效可提示的评估框架
  • 【关于Java的对象】
  • C++、STL面试题总结(二)
  • Android14的QS面板的加载解析
  • 【android bluetooth 协议分析 03】【蓝牙扫描详解 4】【BR/EDR扫描到设备后如何上报给app侧】
  • 力扣137:只出现一次的数字Ⅱ
  • 【计算机网络 | 第4篇】分组交换
  • Javascript/ES6+/Typescript重点内容篇——手撕(待总结)
  • Spring之【初识AOP】
  • hf的国内平替hf-mirror
  • IAR软件中测量函数执行时间
  • 开发笔记 | 接口与抽象基类说明以及对象池的实现