创建一个最基本的QWidget
1.无边框窗口
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
1.背景透明
setAttribute(Qt::WA_TranslucentBackground, true);
如果是5.15以后的版本这里的窗口不是完全透明,会有顶部和左侧两条边框线
解决办法就是重写 paintEvent,把画笔设置透明度为0,5.7就没有这个问题
TestTransparentWidget::TestTransparentWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground, true);
}
TestTransparentWidget::~TestTransparentWidget()
{
}
void TestTransparentWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
QPen pen;
pen.setColor(QColor(0, 0, 0, 0));//透明度为0,高版本qt会有顶部和左侧条边框问题
painter.setPen(pen);
QBrush brush;
brush.setStyle(Qt::SolidPattern); //不设置,背景绘制不出来
brush.setColor(QColor(0, 255, 0, 100));
painter.setBrush(brush);
painter.drawRect(this->rect());
}
画笔设置透明度为0是为了解决高版本qt设置无边框透明背景后仍然会有两条边框线,如果用qss设置可以直接去掉边框
brush.setStyle(Qt::SolidPattern); //不设置,背景绘制不出来
3.鼠标拖动
#ifndef TESTTRANSPARENTWIDGET_H
#define TESTTRANSPARENTWIDGET_H
#include <QtWidgets/QWidget>
#include <QPaintEvent>
#include <QPoint>
#include "ui_testtransparentwidget.h"
#include <QMouseEvent>
class TestTransparentWidget : public QWidget
{
Q_OBJECT
public:
TestTransparentWidget(QWidget *parent = 0);
~TestTransparentWidget();
protected:
virtual void paintEvent(QPaintEvent* event);
virtual void mousePressEvent(QMouseEvent*);
virtual void mouseMoveEvent(QMouseEvent*);
virtual void mouseReleaseEvent(QMouseEvent*);
private:
Ui::TestTransparentWidgetClass ui;
protected:
bool m_bMousePress;
QPoint m_pPress;
};
#endif // TESTTRANSPARENTWIDGET_H
#include "testtransparentwidget.h"
#include <QPen>
#include <QBrush>
#include <QPainter>
#include <QDesktopWidget>
TestTransparentWidget::TestTransparentWidget(QWidget *parent)
: QWidget(parent)
, m_bMousePress(false)
{
ui.setupUi(this);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground, true);
}
TestTransparentWidget::~TestTransparentWidget()
{
}
void TestTransparentWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
QPen pen;
pen.setColor(QColor(0, 0, 0, 0));//透明度为0,高版本qt会有顶部和左侧条边框问题
painter.setPen(pen);
QBrush brush;
brush.setStyle(Qt::SolidPattern); //不设置,背景绘制不出来
brush.setColor(QColor(0, 255, 0, 100));
painter.setBrush(brush);
painter.drawRect(this->rect());
}
void TestTransparentWidget::mousePressEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
{
m_pPress = e->globalPos();
}
m_bMousePress = true;
__super::mousePressEvent(e);
}
void TestTransparentWidget::mouseMoveEvent(QMouseEvent* e)
{
int x = e->x();
int y = e->y();
if (!isMaximized() &&
!isFullScreen() &&
m_bMousePress)
{
QPoint movePos = e->globalPos() - m_pPress + pos();
if (parentWidget())
{
//控制窗口在父窗口内部移动
QRect rect = parentWidget()->geometry();
if (movePos.x() < 0)
{
movePos.setX(0);
}
if (movePos.y() < 0)
{
movePos.setY(0);
}
if (movePos.x() > rect.width() - this->width())
{
movePos.setX(rect.width() - this->width());
}
if (movePos.y() > rect.height() - this->height())
{
movePos.setY(rect.height() - this->height());
}
}
else
{
QRect rect = QApplication::desktop()->availableGeometry();
int topy = rect.y() - this->height() + 30;
if (movePos.y() < topy)
{
movePos.setY(topy);
}
if (movePos.y() > rect.height() - 30)
{
movePos.setY(rect.height() - 30);
}
}
move(movePos);
m_pPress = e->globalPos();
}
__super::mouseMoveEvent(e);
}
void TestTransparentWidget::mouseReleaseEvent(QMouseEvent* e)
{
m_bMousePress = false;
__super::mouseReleaseEvent(e);
}