自定义标题,可拖动

cpp文件

#include "MyTitleWidget.h"

MyTitleWidget::MyTitleWidget(QWidget *parent)
	: QWidget(parent), maxNormal(false), m_parent(nullptr)
{
	ui.setupUi(this);
	connect(ui.minimizeButton, SIGNAL(clicked()), this, SLOT(Slot_TitleButtonClicked()));
	connect(ui.maximizeButton, SIGNAL(clicked()), this, SLOT(Slot_TitleButtonClicked()));
	connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(Slot_TitleButtonClicked()));
	ui.maximizeButton->setProperty("maximizeProperty", "restore");
}

MyTitleWidget::~MyTitleWidget()
{

}

void MyTitleWidget::Slot_TitleButtonClicked()
{
	if (!m_parent)
	{
		return;
	}
	if (this->sender() == ui.minimizeButton)
	{
		m_parent->showMinimized();
	}
	else if (this->sender() == ui.closeButton)
	{
		m_parent->close();
	}
	else if (this->sender() == ui.maximizeButton)
	{
		if (!maxNormal)
		{
			this->sender()->setProperty("maximizeProperty", "maximize");
			m_parent->showMaximized();
		}
		else
		{
			this->sender()->setProperty("maximizeProperty", "restore");
			m_parent->showNormal();
		}
		maxNormal = !maxNormal;
		emit singal_sizeChanged();

	}
}

void MyTitleWidget::mouseDoubleClickEvent(QMouseEvent *e)
{
	if (e->button() == Qt::LeftButton)
	{
		if (maxNormal) 
		{
			if (m_parent)
			{
				m_parent->showNormal();
			}
			maxNormal = !maxNormal;
		}
		else 
		{
			if (m_parent)
			{
				m_parent->showMaximized();
			}
			maxNormal = !maxNormal;
		}

	}

}

void MyTitleWidget::mousePressEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton)
	{
		m_bDrag = true;
		//获得鼠标的初始位置
		mouseStartPoint = event->pos();
		//获得窗口的初始位置
		windowTopLeftPoint = this->geometry().topLeft();
	}
}

void MyTitleWidget::mouseMoveEvent(QMouseEvent *event)
{
	if (m_bDrag)
	{
		//获得鼠标移动的距离
		QPoint distance = event->globalPos() - mouseStartPoint;
		//改变窗口的位置
		m_parent->move(windowTopLeftPoint + distance);
	}
}

void MyTitleWidget::mouseReleaseEvent(QMouseEvent *event)
{
	m_bDrag = false;
}

头文件.h

#pragma once

#include <QWidget>
#include "ui_MyTitleWidget.h"
#include <QMouseEvent>
#include <QDialog>
class MyTitleWidget : public QWidget
{
	Q_OBJECT

public:
	MyTitleWidget(QWidget *parent = Q_NULLPTR);

	~MyTitleWidget();

	void SetParentWidget(QDialog *pDlg){ m_parent = pDlg; }

	void mouseDoubleClickEvent(QMouseEvent *e);

	void mouseMoveEvent(QMouseEvent *event);

	void mouseReleaseEvent(QMouseEvent *);

	void mousePressEvent(QMouseEvent *event);


	public slots:
	void Slot_TitleButtonClicked();

signals:
	void singal_sizeChanged();

private:
	Ui::MyTitleWidget ui;
	bool	maxNormal;
	QDialog  *m_parent;
	bool        m_bDrag;
	QPoint      mouseStartPoint;
	QPoint      windowTopLeftPoint;
};

//UI布局
在这里插入图片描述
然后通过类提升,(右键-提升为-MyTitleWidget)
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Qt 的 QWidget 类来实现自定义标题栏,并且让鼠标可以拖动窗口。 以下是一个简单的示例代码: ```cpp #include <QtWidgets> class CustomTitleBar : public QWidget { public: CustomTitleBar(QWidget *parent = nullptr) : QWidget(parent) { // 设置标题栏大小和背景色 setFixedHeight(30); setStyleSheet("background-color: gray;"); // 添加标题栏上的标题 titleLabel = new QLabel("Custom Title Bar", this); titleLabel->setAlignment(Qt::AlignCenter); titleLabel->setStyleSheet("color: white;"); // 添加关闭按钮 closeButton = new QPushButton("X", this); closeButton->setFixedSize(20, 20); closeButton->setStyleSheet("background-color: red; color: white;"); // 将标题和关闭按钮布局到标题栏 QHBoxLayout *layout = new QHBoxLayout(this); layout->addWidget(titleLabel); layout->addStretch(); layout->addWidget(closeButton); layout->setContentsMargins(5, 0, 5, 0); } // 重写鼠标按下事件,实现窗口的拖动 void mousePressEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { isDragging = true; lastMousePos = event->globalPos(); event->accept(); } } // 重写鼠标移动事件,实现窗口的拖动 void mouseMoveEvent(QMouseEvent *event) override { if (isDragging) { QPoint delta = event->globalPos() - lastMousePos; lastMousePos = event->globalPos(); parentWidget()->move(parentWidget()->pos() + delta); event->accept(); } } // 重写鼠标释放事件,实现窗口的拖动 void mouseReleaseEvent(QMouseEvent *event) override { if (event->button() == Qt::LeftButton) { isDragging = false; event->accept(); } } private: QLabel *titleLabel; QPushButton *closeButton; bool isDragging = false; QPoint lastMousePos; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建窗口并设置标题栏为自定义标题栏 QWidget window; window.setWindowFlags(Qt::FramelessWindowHint); CustomTitleBar *titleBar = new CustomTitleBar(&window); QVBoxLayout *layout = new QVBoxLayout(&window); layout->addWidget(titleBar); layout->addWidget(new QLabel("Hello World!", &window)); window.setLayout(layout); window.resize(300, 200); window.show(); return app.exec(); } ``` 在上述代码中,首先创建了一个名为 `CustomTitleBar` 的自定义窗口部件,它包括一个标题和一个关闭按钮。然后,在 `CustomTitleBar` 类中重写了鼠标按下、移动和释放事件,实现了窗口的拖动。 在 `main` 函数中,创建了一个窗口并设置标题栏为自定义标题栏,然后将自定义标题栏和一个标签添加到窗口中。最后,设置窗口大小并显示窗口。 需要注意的是,为了实现窗口的拖动,需要将窗口的边框隐藏,可以使用 `setWindowFlags(Qt::FramelessWindowHint)` 来实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值