QT 入门(无边框窗口拖拽 窗体透明 阴影)
程序员文章站
2022-03-31 09:38:44
...
1 隐藏window默认边框
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
2 重写鼠标进入事件 鼠标松开函数 鼠标移动事件 声明
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
3 实现
void MainWindow::mouseMoveEvent(QMouseEvent * event)
{
QPoint mousePoint = event->globalPos();
QPoint point = mousePoint - z;
this->move(point);
}
void MainWindow::mousePressEvent(QMouseEvent * event)
{
QPoint mousePoint = event->globalPos();
QPoint topPoint = this->geometry().topLeft();
z = mousePoint - topPoint;
}
void MainWindow::mouseReleaseEvent(QMouseEvent * event)
{
this->z = QPoint();
}
4 设置窗口阴影 无法设置主窗口
QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect();
shadowEffect->setBlurRadius(10); //设置圆角半径 像素
shadowEffect->setColor(Qt::black); // 设置边框颜色
shadowEffect->setOffset(0);
this->ui.shadowWidget->setGraphicsEffect(shadowEffect);
5 设置主窗口透明
this->setAttribute(Qt::WA_TranslucentBackground);