Qt之无边框自定义最小化、关闭按钮并实现窗口移动及伸缩窗口

如下图:

无边框界面只需继承下面的基类即可。 

WinNoFrameUI.h

#ifndef WINNOFRAMEUI_H
#define WINNOFRAMEUI_H

#include <QWidget>
#include <QLabel>
#include <QFrame>

class QToolButton;

#define MARGIN 5			// 四个角的长度,伸缩窗口时用到

class WinNoFrameUI : public QWidget
{
	Q_OBJECT

public:
	WinNoFrameUI(QWidget *parent = NULL);
	~WinNoFrameUI();

	void SetWindowTitle(const QString title);
	void SetWindowTitleIcon(const QIcon icon);
	void SetWindowTitleColor(const QString sColor);
	void CreateWindowButton();

	int countFlag(QPoint p, int row);			// 计算鼠标在哪一列和哪一行
	void setCursorType(int flag);				// 根据鼠标所在位置改变鼠标指针形状
	int countRow(QPoint p);						// 计算在哪一列

protected:
	virtual void mousePressEvent(QMouseEvent *event);
	virtual void mouseDoubleClickEvent(QMouseEvent *event);
	virtual void mouseMoveEvent(QMouseEvent *event);
	virtual void mouseReleaseEvent(QMouseEvent *event);

private slots:
	void showMinWindow();
	void showMaxWindow();

public:
	QWidget		* m_pWidget;
	QToolButton	* m_pTitleTBtn;				// 窗口标题
	QToolButton * m_pMinTBtn;				// 最小化按钮
	QToolButton * m_pMaxTBtn;				// 最大化按钮
	QToolButton * m_pCloseTBtn;				// 关闭按钮
	QFrame		* m_pLine;					// 标题栏分割线

private:
	bool		m_Drag;						//判断鼠标左键是否按下
	QPoint		m_DragPosition;				// 用于移动窗口
	bool		m_bMoved;					// true:正在移动
	int			curPos;						// 用于判断是移动窗口还是伸缩窗口
};

#endif // WINNOFRAMEUI_H
WinNoFrameUI.cpp
#include "WinNoFrameUI.h"

#include <QToolButton>
#include <QMouseEvent>
#include <QStyle>
#include <QHBoxLayout>
#include <QApplication>
#include <QDesktopWidget>
#include <QDebug>

WinNoFrameUI::WinNoFrameUI(QWidget *parent)
: QWidget(parent)
{
	m_bMoved = false;

	this
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您讲解如何在QT实现定义窗口的拖拽移动。 首先,我们需要重写QWidget的mousePressEvent和mouseMoveEvent两个事件函数。 在mousePressEvent中,我们需要记录下鼠标按下时的坐标和窗口左上角的坐标,以便计算窗口移动的偏移量。 在mouseMoveEvent中,我们需要计算鼠标移动的偏移量,并将窗口的位置进行相应的调整。 以下是示例代码: ```cpp void MyWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragPosition = event->globalPos() - frameGeometry().topLeft(); } } void MyWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { QPoint newPos = event->globalPos() - m_dragPosition; move(newPos); } } ``` 在上面的代码中,m_dragPosition是一个QPoint类型的成员变量,用于记录鼠标按下时的坐标和窗口左上角的坐标的差值。 最后,我们需要在窗口的构造函数中设置窗口的样式为Qt::FramelessWindowHint,以去掉窗口边框和标题栏。 完整的示例代码如下: ```cpp #include "mywindow.h" #include <QMouseEvent> MyWindow::MyWindow(QWidget *parent) : QWidget(parent) { setFixedSize(400, 300); setWindowFlags(Qt::FramelessWindowHint); } void MyWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragPosition = event->globalPos() - frameGeometry().topLeft(); } } void MyWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { QPoint newPos = event->globalPos() - m_dragPosition; move(newPos); } } ``` 希望这个简单的示例对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值