当前位置: 首页 > 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.dtcms.com/a/317181.html

相关文章:

  • 基于最大似然估计的卡尔曼滤波与自适应模糊PID控制的单片机实现
  • 鼠标下滑时回跳问题
  • 从“更优”到“更智”:V5.7.3 的交互革新、模式扩展与体验跃迁
  • NodeJs学习日志(1):windows安装使用node.js 安装express,suquelize,sqlite,nodemon
  • ESP32:2.搭建UDP服务器
  • 参考线程池构建一个高性能、配置驱动的Docker容器池
  • Linux---第二天---基础指令
  • copy_file_range系统调用及示例
  • 使用Nginx部署前后端分离项目
  • Docker的安装,服务器与客户端之间的通信
  • Linux基础命令的生产常用命令及其示例简单解释
  • 电子电气架构 ---如何焕新升级为 48V 电气架构
  • 【32】C++实战篇—— m行n列的坐标点,求每行相邻点X差值dX,每列相邻点y差值dY,并以矩阵形式左端对齐
  • 玩转 InfluxDB 3:用 HTTP API 快速创建高效数据表
  • Python科学计算:从基础到工程仿真的完整指南
  • 【java】DDD架构同普通微服务项目的区别
  • 23.统计分析:从数据中挖掘洞察
  • 深入浅出 RabbitMQ - 主题模式(Topic)
  • JavaWeb(04)
  • 丝滑qt页面跳转
  • android10~16变更一览和开发者兼容应对
  • 学习 Android(十五)NDK进阶及性能优化
  • antd组件select下拉数据分页加载
  • 学习 Android (十六) 学习 OpenCV (一)
  • Cglib的Enhancer实现动态代理?
  • 新能源汽车热管理系统核心零部件及工作原理详解
  • AI巨模型对决2025:五强争霸,谁能称王?
  • ORACLE 19C建库时卡在46%、36%
  • 【网络运维】Linux:简单DHCP服务器的部署
  • PyTorch入门引导