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