Qt - 在不使用 WA_TranslucentBackground 的情况下在 Windows 中绘制完全透明的 window
Qt - Draw a fully transparent window in Windows without using WA_TranslucentBackground
我需要绘制透明 window(QLabel
或 QFrame
或 QWidget
),但不使用 WA_TranslucentBackground
。原因是 windows 将包含通过 OpenGL 呈现的其他子窗口小部件,并且使用 属性 会使那些 windows 在 Windows 上不可见,如记录 here.虽然这在 Mac 中工作正常,但我只需要在 Windows 上使用不同的解决方案,因为它在那里不起作用。我试过这个:设置一个空白 pixmap
。但它仍然显示为灰色背景:
#include <QApplication>
#include <QLabel>
#include <QBitmap>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel l;
l.setWindowFlags(Qt::FramelessWindowHint);
QPixmap p("");
l.setPixmap(p);
l.setScaledContents(true);
l.resize(300, 500); //just to test my idea
l.setMask(p.scaled(l.width(),l.height(),Qt::IgnoreAspectRatio,
Qt::SmoothTransformation).mask());
l.show();
return a.exec();
}
任何人都可以建议在 Windows 上实现此目的的任何其他方法,即完全透明的 window?平台 - Qt 5.3.1,32 位。
P.S - 它不需要表现得像半透明 window,即可以通过 WA_TranslucentBackground
呈现的小部件的透明部分点击背景。这里只要是透明的就可以,不需要可以点击'through'.
我正在使用 Windows (Qt 5) 并使用以下内容创建一个 Semi-Transparent 小部件:
setWindowOpacity(0.6);
setStyleSheet("QWidget{background: #000000}")
如果将不透明度设置为零,它应该可以工作。
我将它与 Windows 标志 "framelessWindow" 和 "Tool" 一起使用来使屏幕背景变暗。
不幸的是,这在 Windows 上是不可能的。您可以使用 alpha-channel 为带颜色的小部件设置透明度,如下所示:setStyleSheet("background-color: rgba(255,0,0,50%);");
。但是在您设置 WA_TranslucentBackground
之前,whis 不适用于顶级小部件。没有这个标志 alpha-channel 不起作用,你会变黑 window。所以唯一的解决办法是使用WA_TranslucentBackground
。然后像 Qt 文档说的那样做 OpenGL 绘画:
To work around this issue you can either just use Qt to render
everything and not OpenGL, or you can render the OpenGL into a pixmap
and draw that onto your widget instead.
透明度可以通过启用在window后面模糊并通过将windows 属性设置为WA_TranslucentBackground
setAttribute(Qt::WA_TranslucentBackground);
QtWin::enableBlurBehindWindow(this);
需要,cpp 包括:include ,项目文件包括:QT += winextras
enableBlurBehindWindow() 方法还有两个重载,您可以在 this 文档
上查看它们
对于子窗口小部件,您可以使用
setStyleSheet("background:transparent");
我需要绘制透明 window(QLabel
或 QFrame
或 QWidget
),但不使用 WA_TranslucentBackground
。原因是 windows 将包含通过 OpenGL 呈现的其他子窗口小部件,并且使用 属性 会使那些 windows 在 Windows 上不可见,如记录 here.虽然这在 Mac 中工作正常,但我只需要在 Windows 上使用不同的解决方案,因为它在那里不起作用。我试过这个:设置一个空白 pixmap
。但它仍然显示为灰色背景:
#include <QApplication>
#include <QLabel>
#include <QBitmap>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel l;
l.setWindowFlags(Qt::FramelessWindowHint);
QPixmap p("");
l.setPixmap(p);
l.setScaledContents(true);
l.resize(300, 500); //just to test my idea
l.setMask(p.scaled(l.width(),l.height(),Qt::IgnoreAspectRatio,
Qt::SmoothTransformation).mask());
l.show();
return a.exec();
}
任何人都可以建议在 Windows 上实现此目的的任何其他方法,即完全透明的 window?平台 - Qt 5.3.1,32 位。
P.S - 它不需要表现得像半透明 window,即可以通过 WA_TranslucentBackground
呈现的小部件的透明部分点击背景。这里只要是透明的就可以,不需要可以点击'through'.
我正在使用 Windows (Qt 5) 并使用以下内容创建一个 Semi-Transparent 小部件:
setWindowOpacity(0.6);
setStyleSheet("QWidget{background: #000000}")
如果将不透明度设置为零,它应该可以工作。 我将它与 Windows 标志 "framelessWindow" 和 "Tool" 一起使用来使屏幕背景变暗。
不幸的是,这在 Windows 上是不可能的。您可以使用 alpha-channel 为带颜色的小部件设置透明度,如下所示:setStyleSheet("background-color: rgba(255,0,0,50%);");
。但是在您设置 WA_TranslucentBackground
之前,whis 不适用于顶级小部件。没有这个标志 alpha-channel 不起作用,你会变黑 window。所以唯一的解决办法是使用WA_TranslucentBackground
。然后像 Qt 文档说的那样做 OpenGL 绘画:
To work around this issue you can either just use Qt to render everything and not OpenGL, or you can render the OpenGL into a pixmap and draw that onto your widget instead.
透明度可以通过启用在window后面模糊并通过将windows 属性设置为WA_TranslucentBackground
setAttribute(Qt::WA_TranslucentBackground);
QtWin::enableBlurBehindWindow(this);
需要,cpp 包括:include
enableBlurBehindWindow() 方法还有两个重载,您可以在 this 文档
上查看它们对于子窗口小部件,您可以使用
setStyleSheet("background:transparent");