gpt4 book ai didi

windows - 使用 Qt 显示半透明/不规则形状的窗口

转载 作者:可可西里 更新时间:2023-11-01 12:35:13 26 4
gpt4 key购买 nike

是否可以使用 Qt 显示半透明和/或不规则形状的窗口?

(我假设它最终取决于底层 GUI 系统的功能,但我们假设至少是 Windows XP/Mac OS X)

如果是这样,如何做到这一点?

最佳答案

是的,这是可能的。关键是QWidget

Qt::WA_TranslucentBackground属性

这是一个简单的类,它绘制了一个带有红色背景 50% alpha 的圆形半透明窗口。

TranslucentRoundWindow.h:

#include <QWidget>

class TranslucentRoundWindow : public QWidget
{
public:
TranslucentRoundWindow(QWidget *parent = 0);
virtual QSize sizeHint() const;

protected:
virtual void paintEvent(QPaintEvent *paintEvent);
};

TranslucentRoundWindow.cpp:

#include <QtGui>

#include "TranslucentRoundWindow.h"

TranslucentRoundWindow::TranslucentRoundWindow(QWidget *parent) : QWidget(parent, Qt::FramelessWindowHint)
{
setAttribute(Qt::WA_TranslucentBackground);
}

QSize TranslucentRoundWindow::sizeHint() const
{
return QSize(300, 300);
}

void TranslucentRoundWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(255, 0, 0, 127));

painter.drawEllipse(0, 0, width(), height());
}

如果您希望能够使用鼠标移动此窗口,则必须覆盖 mousePressEventmouseMoveEventmouseReleaseEvent

关于windows - 使用 Qt 显示半透明/不规则形状的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1333610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com