Qt无边框半透明窗口-鼠标移动-绘制背景色不起效

创建一个最基本的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);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值