废话不多说,直接上代码
frameless.h
// "frameless.cpp"
// 参考
// https://www.cnblogs.com/findumars/p/6209705.html
// https://www.cnblogs.com/linuxAndMcu/p/10609182.html#_label0
// https://blog.csdn.net/haodemei2010/article/details/45533809
#ifndef FRAMELESS_H
#define FRAMELESS_H 1
//
#include<QWidget>
#include<QLabel>
#define TITLE_BAR 1
#if TITLE_BAR != 0
#include<QToolButton>
#include<QLabel>
#include<QDir>
#include<QStyle>
#endif
class Widget: public QWidget {
Q_OBJECT
public: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QLabel *ll;
explicit Widget(QWidget *parent = nullptr);
// fl == frameless 缩放 {
{
{
QPoint fl_mpoint; // 按下鼠标时的位置 mouse point
int fl_fw = 10, // 边框宽度 frame width
fl_mh = 100; // 最小化 鼠标右键下拖距离 minimum height
QRect fl_tr; // this矩形 this rect
int screen_w, screen_h, screen_p; // 宽,高,一份。width, height, portion
enum Efl_direction {
Center=0, Top, Bottom, Left, Right,
TopLeft, TopRight, BottomLeft, BottomRight //, Other
};
enum Efl_mouse_press {
MP_null=0, MP_left, MP_right };
Efl_direction fl_direction = Center; // 方向
Efl_mouse_press fl_mpress = MP_null; // 鼠标按键 mouse press
void fl_set_cursor(const QPoint &pos); // 设置指针
bool eventFilter(QObject *obj,QEvent *event) override; // 缩放 end }}}
#if TITLE_BAR != 0
QWidget *t_title_bar; // 标题栏 隐藏标题栏 {
{
{
QLabel *t_title; // 标题
QToolButton *t_close, // 关闭
*t_max, // 最大化/还原
*t_min; // 最小化
bool t_show; // 已显示?
void title_init(void); // 标题栏 初始化
void title_resize(void); // 标题栏 resize
void title_show(void); // 标题栏 显示
void title_hide(void); // 标题栏 隐藏 隐藏标题栏 end }}}
#endif
};
//
#endif
frameless.cpp
#include"frameless.h"
//#include"main.cpp"
//#include"frameless.pro"
#include<QApplication>
#include<QScreen>
#include<QMouseEvent>
#ifndef QT_NO_DEBUG
#include<QDebug>
#endif
// flag_qt_bug qt的bug
Widget::Widget(QWidget *parent): QWidget(parent) { // {
{
{
QSize screen_size = QGuiApplication::screens().first()->size();
screen_w = screen_size.width();
screen_h = screen_size.height();
screen_p = ((screen_h < screen_w)? screen_h/108 : screen_w/108);
// this_ = this;
fl_fw = screen_p; // 边框宽度 frame width
fl_mh = 10*screen_p; // 最小化 鼠标右键下拖距离 minimum height
this->setWindowFlags(Qt::FramelessWindowH