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

it2023-11-18  82

 

 

实现阴影效果需要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); } }

 

最新回复(0)