QT笔记之不规则窗口的实现

时间:2023-03-10 00:25:21
QT笔记之不规则窗口的实现

QT实现的不规则窗口,是根据图片的形状显示

1.去标题栏

2.设置窗口背景为透明色

3.最后给窗口设置背景色

注:背景图为镂空的 格式为.png

图片资源下载:http://pan.baidu.com/s/1i5JkIot

.h

 #ifndef QANORMALYDLG_H
#define QANORMALYDLG_H #include <QWidget>
#include "ui_qanormalydlg.h" class QAnormalyDlg : public QWidget
{
Q_OBJECT public:
QAnormalyDlg(QWidget *parent = );
~QAnormalyDlg(); void paintEvent(QPaintEvent *e); void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e);
private:
Ui::QAnormalyDlg ui; QPoint move_point; //移动的距离
}; #endif // QANORMALYDLG_H

.cpp

 #include "qanormalydlg.h"
#include <QPainter>
#include <QMouseEvent> QAnormalyDlg::QAnormalyDlg(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this); //去表框 同时保留窗口原有的属性
setWindowFlags(Qt::FramelessWindowHint | windowFlags() );

//把窗口背景设为透明
setAttribute(Qt::WA_TranslucentBackground);

resize(,);
} QAnormalyDlg::~QAnormalyDlg()
{ } void QAnormalyDlg::paintEvent(QPaintEvent *e)
{
QPainter p(this);
p.drawPixmap(, , QPixmap("122.png"));
} void QAnormalyDlg::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::RightButton)
{
//右键关闭窗口
close();
} else if (e->button() == Qt::LeftButton)
{
//求坐标差,当前鼠标坐标 - 窗口左上角坐标
//frameGeometry返回窗口的矩形坐标, topLeft返回窗口左上角点的坐标
//move_point = e->globalPos() - this->frameGeometry().topLeft();
move_point = e->globalPos() - this->pos();
}
} void QAnormalyDlg::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons() & Qt::LeftButton)
{
move(e->globalPos()-move_point);
}
}

效果:

QT笔记之不规则窗口的实现