Qt 去边框,去掉边框之后窗体移动,阴影,最大化和还原的实现

1、Qt去边框
构造函数中加上如下代码:
this->setWindowFlags(Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);

2、去掉边框之后窗体移动
在.h文件中加上

#include
#include

protected:
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
QPoint last;

在.cpp上加上如下代码:

void Widget::mousePressEvent(QMouseEvent *e)
{
last = e->globalPos();
}

void Widget::mouseMoveEvent(QMouseEvent *e)
{
int dx = e->globalX() - last.x();
int dy = e->globalY() - last.y();
last = e->globalPos();
move(x()+dx, y()+dy);
}

void Widget::mouseReleaseEvent(QMouseEvent *e)
{
int dx = e->globalX() - last.x();
int dy = e->globalY() - last.y();
move(x()+dx, y()+dy);
}

3、阴影的实现

在.h中加如下代码:

#include

protected:
void paintEvent(QPaintEvent *event);
QPoint last;
int w = 10;

在.cpp加上如下代码:
构造函数中加上
setAttribute(Qt::WA_TranslucentBackground);

void Widget::paintEvent(QPaintEvent event) // 绘图
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect( w, w, this->width()- w
2, this->height() - w*2 ); //

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(Qt::white));

QColor color(0, 0, 0, 19);
for(int i=0; iwidth()-(w-i)*2, this->height()-(w-i)*2);
    color.setAlpha( 21 - qSqrt(i)*7);
    painter.setPen(color);
    painter.drawPath(path);
}

}

4、最大/还原的实现
1)在头文件加上如下代码
// 最大化/还原的标志
int Maxflag = 0; // 0 正常大小 1 最大化
2、在对应按钮的槽函数地方加上如下代码

Maxflag = 1 - Maxflag;
if( Maxflag == 0 )
{
    w = 10;   // 去掉虚边
    this->showNormal();
}
else if( Maxflag == 1 )
{
    w = 0;   // 去掉虚边
    this->showFullScreen();
}

实例代码:

.h的源码

#ifndef WIDGET_H
#define WIDGET_H

#include
#include
#include
#include

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = nullptr);
~Widget();

protected:
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);

void paintEvent(QPaintEvent *event);

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

private:
Ui::Widget *ui;
QPoint last;
int w = 10;

//  最大化/还原的标志
int Maxflag = 0;    // 0 正常大小 1 最大化

};

#endif // WIDGET_H

.cpp的源码

#include “widget.h”
#include “ui_widget.h”

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);

this->setWindowFlags(Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);

}

Widget::~Widget()
{
delete ui;
}

void Widget::mousePressEvent(QMouseEvent *e)
{
last = e->globalPos();
}

void Widget::mouseMoveEvent(QMouseEvent *e)
{
int dx = e->globalX() - last.x();
int dy = e->globalY() - last.y();
last = e->globalPos();
move(x()+dx, y()+dy);
}

void Widget::mouseReleaseEvent(QMouseEvent *e)
{
int dx = e->globalX() - last.x();
int dy = e->globalY() - last.y();
move(x()+dx, y()+dy);
}

void Widget::paintEvent(QPaintEvent event) // 绘图
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect( w, w, this->width()- w
2, this->height() - w*2 ); //

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(Qt::white));

QColor color(0, 0, 0, 19);
for(int i=0; iwidth()-(w-i)*2, this->height()-(w-i)*2);
    color.setAlpha( 21 - qSqrt(i)*7);
    painter.setPen(color);
    painter.drawPath(path);
}

}

void Widget::on_pushButton_clicked()
{
Maxflag = 1 - Maxflag;
if( Maxflag == 0 )
{
w = 10; // 去掉虚边
this->showNormal();
}
else if( Maxflag == 1 )
{
w = 0; // 去掉虚边
this->showFullScreen();
}
}

void Widget::on_pushButton_2_clicked()
{
// 关闭窗体
this->close();
}

你可能感兴趣的:(Qt,qt,去边框,窗体移动,阴影,最大化和还原)