QT 无边框界面和阴影以及拖动

无边框界面

这是一个无边框的窗口

设置无边框

MainWindow::MainWindow(QWidget *parent) :
        QWidget(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);
**    this->setWindowFlags(Qt::FramelessWindowHint);**

}

拖拽和阴影

拖拽窗口

头文件实现父类QWidget的三个方法

#include <QPoint>
#include<QMouseEvent>
#include <QDebug>

....
//重写父类接口
public:
    void mouseMoveEvent(QMouseEvent *event) override;

    void mousePressEvent(QMouseEvent *event) override;

    void mouseReleaseEvent(QMouseEvent *event) override;

.cpp

//如果重写的方法什么不做,那么对应的功能是没有的,这里还是需要调用父类的方法
//鼠标移动事件
void MainWindow::mouseMoveEvent(QMouseEvent *event) {
    QWidget::mouseMoveEvent(event);
    qDebug()<< "鼠标开始<移动>";
    QPoint y = event->globalPos();//鼠标相对于桌面左上角的位置,鼠标全局位置
    QPoint x = y - this->z;
    this->move(x);
}

//鼠标按下事件
void MainWindow::mousePressEvent(QMouseEvent *event) {
    QWidget::mousePressEvent(event);
    qDebug()<< "鼠标开始<按下>";

    QPoint y = event->globalPos();//鼠标相对于桌面左上角的位置,鼠标全局位置
    QPoint x = this->geometry().topLeft();//窗口左上角相对于桌面左上角的位置,窗口位置
    this->z = y - x; // 不变的定值
}

//释放事件
void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
    QWidget::mouseReleaseEvent(event);
    qDebug()<< "鼠标<释放>";
    this->z = QPoint();
}

添加阴影

#include "QGraphicsDropShadowEffect"
...

    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect();
    shadowEffect->setBlurRadius(7);
    shadowEffect->setColor(Qt::black);
    shadowEffect->setOffset(0);
    ui->widget->setGraphicsEffect(shadowEffect);
    
  //设置窗口透明
  this->setAttribute(Qt::WA_TranslucentBackground);
posted @ 2022-08-16 15:34  Amani_Bey  阅读(284)  评论(0)    收藏  举报