Qt 无边框 移动 缩放,隐藏标题栏,鼠标控制最小化,最大化,关闭

本文介绍如何使用Qt实现一个无边框窗口,并通过鼠标操作实现窗口的移动、缩放、最小化、最大化和关闭功能。提供相关的frameless.h和frameless.cpp代码示例。
摘要由CSDN通过智能技术生成

废话不多说,直接上代码

 

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值