新建一个阴影类,将这个类作为阴影背景
.h
class ShadowWindow : public QWidget
{
Q_OBJECT
public:
ShadowWindow(QWidget *parent=0);
~ShadowWindow();
protected:
void paintEvent(QPaintEvent* event);
};
.cpp
#include "stdafx.h"
#include "ShadowWindow.h"
#include"PWindow.h"
ShadowWindow::ShadowWindow(QWidget *parent)
: QWidget(parent)
{
this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
this->resize(620, 420);//背景大于子窗口一点
setAttribute(Qt::WA_TranslucentBackground);
PWindow* pw = new PWindow(this);//子窗口
pw->show();
}
ShadowWindow::~ShadowWindow()
{
}
void ShadowWindow::paintEvent(QPaintEvent* event) {
int r = 170, g = 170, b = 170, a = 0;
int x = 0, y = 0, w = this->width(), h = this->height();
for (int i = 0; i <= 10; i++) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QColor(r - i * 15, g - i * 15, b - i * 15, a + pow(i,2.1)));
//painter.drawRoundedRect(QRect(x + i, y + i, w - (i * 2), h - (i * 2)), 2, 2);
painter.drawRect(QRect(x + i, y + i, w - (i * 2), h - (i * 2)));
painter.end();
}
}
如果子窗口覆盖了这个窗口,可以通过子窗口的拖动函数,调用move函数来移动父窗口,使得父窗口跟随子窗口移动,在子窗口要传入父窗口指针,子窗口类中定义指针接收父指针,这样就能操作父窗口。
QWidget* qw;//类中
qw = parent;//构造函数中
void PWindow::mouseMoveEvent(QMouseEvent* event){
if (isMousePressed == true)
{
QPoint topLeftPoint = event->globalPos() - mousePosition;
this->move(topLeftPoint);
qw->move(this->x()-10,this->y()-10);//这里要做一下偏移,因为要流出阴影。
}
}
网友评论