QT 设计透明窗口(视频上方)

头文件

class MyAutoCloseFrameBase : public QFrame
{
	Q_OBJECT

public:
	MyAutoCloseFrameBase(QWidget *parent,int keeptime = 2000);//msec
	~MyAutoCloseFrameBase();

	void resetAutoClock(bool isable = true);
	void SetLabelText(QString text);
protected:
	bool event(QEvent *e);
	void paintEvent(QPaintEvent *ev);
private:
	QTimer*		m_pTimer;
	int			m_keeptime;
	QVBoxLayout*	m_QVBoxLayout;
	QLabel*			m_label;
};

cpp文件

#include "MyAutoCloseFrameBase.h"

MyAutoCloseFrameBase::MyAutoCloseFrameBase(QWidget *parent,int keeptime)
	: QFrame(parent)
	, m_keeptime(keeptime)
{
	setWindowFlags( Qt::FramelessWindowHint|Qt::Tool);
	setAttribute(Qt::WA_TranslucentBackground);
	//设置为Qt::Tool属性才可以让透明窗口浮动在上方,并且不阻塞信号
	//设置为Qt::Dialog属性会阻塞事件,导致快捷键不可用

	m_pTimer = new QTimer(this);
	m_pTimer->setSingleShot(true);//start once timeout once
	m_pTimer->setInterval(m_keeptime);
	connect(m_pTimer, SIGNAL(timeout()),this,SLOT(hide()));
	//设置定时器自动隐藏

	m_label = new QLabel(this);
	m_label->setAlignment(Qt::AlignCenter);
	m_QVBoxLayout = new QVBoxLayout(this);
	m_QVBoxLayout->setContentsMargins(12, 9, 12, 9);
	m_QVBoxLayout->setSpacing(0);
	m_QVBoxLayout->addWidget(m_label);
	setLayout(m_QVBoxLayout);
}

MyAutoCloseFrameBase::~MyAutoCloseFrameBase()
{
}
void MyAutoCloseFrameBase::resetAutoClock(bool isable)
{
	m_pTimer->start(m_keeptime);
}
bool MyAutoCloseFrameBase::event(QEvent* e)
{
	if (e->type() == QEvent::HoverMove)
	{
		resetAutoClock(false);
	}
	if (e->type() == QEvent::Show)
	{
		m_pTimer->start();
		return false;
	}
	return QFrame::event(e);
}
void MyAutoCloseFrameBase::SetLabelText(QString text)
{
	m_label->setText(text);
}
void MyAutoCloseFrameBase::paintEvent(QPaintEvent*ev)
{
	QPainter p(this);
	p.setPen(Qt::NoPen);

	/*透明背景*/
	p.setBrush(QBrush(QColor(0, 0, 0, 0)));
	p.drawRect(rect());
	p.setRenderHint(QPainter::Antialiasing);
	p.setRenderHint(QPainter::SmoothPixmapTransform, true);

	/*圆角矩形背景*/
	p.setBrush(QBrush(QColor(75,88,94,255)));
	p.drawRoundedRect(0, 0, width() - 1, height() - 1, 6 * g_dscale, 6 * g_dscale);

	/*边框*/
	p.setPen(QColor(38, 49, 55, 255));
	p.drawRoundedRect(0, 0, width() - 1, height() - 1, 6 * g_dscale, 6 * g_dscale);
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值