Qt去除标题栏后的窗口的移动和阴影效果

 

 

实现阴影效果需要include <QGraphicsDropShadowEffect>,因此要在Qt设置上给Windows Extras打上勾。原理则是嵌套一个QWidget,新建的QWidget上放上原有的控件,作为原来的背景,而原来的背景则用于显示阴影的效果,阴影大小则为两个背景的间距。移动方面则是通过重新实现Qt中的那几个鼠标相关的虚函数。

 

QtTest2.ui 

QtTest2.h

#include <QtWidgets/QWidget>
#include "ui_QtTest2.h"

class QtTest2 : public QWidget
{
    Q_OBJECT

public:
    QtTest2(QWidget *parent = Q_NULLPTR);

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

private:
	Ui::QtTest2Class ui;

	bool m_mousePressed;
	QPoint m_pos;    //全局位置  
};

QtTest2.cpp 

#include "QtTest2.h"

#include <QMouseEvent>
#include <QGraphicsDropShadowEffect>

QtTest2::QtTest2(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

	this->setAttribute(Qt::WA_TranslucentBackground, true);
	this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
	QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect(this);
	shadow->setOffset(0, 0);
	shadow->setColor(QColor("#444444"));
	shadow->setBlurRadius(20);	//设置阴影圆角
	ui.background->setGraphicsEffect(shadow);
}

void QtTest2::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		m_mousePressed = true;
		m_pos = event->globalPos() - pos();
	}
}

void QtTest2::mouseReleaseEvent(QMouseEvent* event)
{
	m_mousePressed = false;
}

void QtTest2::mouseMoveEvent(QMouseEvent* event)
{
	if (m_mousePressed)
	{
		move(event->globalPos() - m_pos);
	}
}

 

以下是去除标题栏并实现移动和缩放界面的Qt代码: mainwindow.h ```cpp #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); protected: void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; void resizeEvent(QResizeEvent *event) override; private: QPoint m_dragPos; bool m_dragging; }; #endif // MAINWINDOW_H ``` mainwindow.cpp ```cpp #include "mainwindow.h" #include <QMouseEvent> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); } void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragPos = event->globalPos() - frameGeometry().topLeft(); m_dragging = true; event->accept(); } } void MainWindow::mouseMoveEvent(QMouseEvent *event) { if (m_dragging && event->buttons() & Qt::LeftButton) { move(event->globalPos() - m_dragPos); event->accept(); } } void MainWindow::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragging = false; event->accept(); } } void MainWindow::resizeEvent(QResizeEvent *event) { Q_UNUSED(event); update(); } ``` 在这个示例中,我们使用 `setWindowFlags()` 去除标题栏,并使用 `setAttribute(Qt::WA_TranslucentBackground)` 设置透明背景。然后,在 `mousePressEvent()`、`mouseMoveEvent()` 和 `mouseReleaseEvent()` 函数中实现了移动窗口的功能。在 `resizeEvent()` 函数中,我们使用 `update()` 函数通知窗口重新绘制,以便在缩放窗口时更新窗口内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值