I have small problem with QOpenGLWidget
and its background color.
我对QOpenGLWidget和它的背景颜色有一点小问题。
When I want to create semi-transparent rect on my custom QOpenGLWidget
using QPainter
there are 2 different results:
当我想使用QPainter在我的自定义QOpenGLWidget上创建半透明的RECT时,有两种不同的结果:
- If
MyCustomWidget
have parent - on every update rect's color multiplies (and after few repaints it is opaque, like previous painting result not cleaned)
- If
MyCustomWidget
doesn't have parent - color doesn't repaints each time
Here is code example for QPainter
:
以下是QPainter的代码示例:
class Widget : public QOpenGLWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0)
: QOpenGLWidget(parent)
{
resize(800, 600);
Test *test = new Test(this);
}
~Widget(){}
protected:
void paintEvent(QPaintEvent *) {}
protected:
void initializeGL() {
if(paintEngine()->type() != QPaintEngine::OpenGL &&
paintEngine()->type() != QPaintEngine::OpenGL2)
qDebug() << "ERROR. Type is: " << paintEngine()->type();
}
void resizeGL(int, int) {}
void paintGL() {
QPainter p;
p.begin(this);
{
p.fillRect(rect(), Qt::white);
}
p.end();
}
private:
class Test : public QOpenGLWidget
{
public:
Test(QWidget *parent = 0) : QOpenGLWidget(parent) {
resize(100, 100);
}
protected:
void paintEvent(QPaintEvent *) {
QPainter p(this);
p.fillRect(rect(), QColor(125, 125, 125, 255/10));
}
};
};
Also by default it has black background (I don't know how to fix it. setAttribute(Qt::WA_TranslucentBackground)
doesn't helps).
默认情况下,它的背景是黑色的(我不知道如何修复它。SetAttribute(qt::WA_TransLucentBackround)没有帮助)。
Also, when I'm trying to clear color using glClear
it ignores alpha (both on QOpenGLWidget
with parent and not). Here is Test
class from previous code, but now it is using opengl to clear color:
此外,当我尝试使用glClear清除颜色时,它忽略了Alpha(在QOpenGLWidget上都有父级和非父级)。下面是前面代码中的测试类,但现在它使用OpenGL来清除颜色:
class Test : public QOpenGLWidget
{
public:
Test(QWidget *parent = 0) : QOpenGLWidget(parent) {
resize(100, 100);
}
void initializeGL() {
QOpenGLFunctions *f = context()->functions();
f->glClearColor(0.0f, 1.0f, 0.0f, 0.1f);
}
void paintGL() {
QOpenGLFunctions *f = context()->functions();
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
};
How can I fix this problems?
我如何解决这个问题?
I'm using Qt 5.5.0, Windows 10, MinGW 4.9.2
我使用的是Qt 5.5.0、Windows 10、MinGW 4.9.2
更多回答
优秀答案推荐
Xeed is correct when saying the QOpenGLWidget is painted first.
Xeed说QOpenGLWidget是首先绘制的,这是正确的。
I'm not an expert but I think I found the solution. You need to set a widget attribute to always make the widget stacked on top (think of the widgets as layers on the window). Here is a link to where I got the following information:
我不是专家,但我想我找到了解决方案。您需要设置一个小部件属性,以始终使小部件堆叠在顶部(将小部件视为窗口上的层)。以下是我从哪里获得以下信息的链接:
P.S. As mentioned in the QQuickWidget post, there is a limitation regarding semi-transparency when using QQuickWidget or QOpenGLWidget as child widgets. For applications that absolutely need this, Qt 5.4 offers a workaround: the newly introduced Qt::WA_AlwaysStackOnTop widget attribute. This, at the expense of breaking the stacking order for other types of layouts, makes it possible to have a semi-transparent QQuickWidget or QOpenGLWidget with other widgets visible underneath. Of course, if the intention is only to make other applications on the desktop visible underneath, then the Qt::WA_TranslucentBackground attribute is sufficient
Solution in Python:
在Python中的解决方案:
set attribute of OpenGL widget
setAttribute(Qt.WA_AlwaysStackOnTop)
设置OpenGL控件的属性setAttribute(Qt.WA_Alway sStackOnTop)
Now the OpenGL widget is considered 'on top' in the window. Use 'glClearColor' function and specify the alpha channel to be zero (0.0).
现在,OpenGL小部件在窗口中被认为是“最上面的”。使用‘glClearColor’函数并将Alpha通道指定为零(0.0)。
glClearColor(0.0, 0.0, 0.0, 0.0)
GlClearColor(0.0,0.0,0.0,0.0)
I'm not sure how to write that in other languages but this worked for me. The OpenGL widget no longer has the default black background. It is transparent! Hope this helps.
我不确定如何用其他语言来写,但这对我很有效。OpenGL小工具不再具有默认的黑色背景。它是透明的!希望这能帮上忙。
As far as I know the QOpenGLWidget is always drawn first. Therefore you cannot show any widgets layered below. I'm currently looking into the same issue. I'll report back, when I find any solution.
据我所知,QOpenGLWidget总是首先绘制的。因此,您不能显示下面分层的任何小部件。我目前正在调查同样的问题。当我找到任何解决办法时,我会报告的。
I've had similar issue with QOpenGLWidget
not repainting correctly in transparent areas and decided to switch to QOpenGLWindow
wrapped inside QWidget::createWindowContainer()
我也遇到过类似的问题,QOpenGLWidget无法在透明区域正确重绘,因此决定切换到包装在QWidget::createWindowContainer()中的QOpenGLWindow。
更多回答
我是一名优秀的程序员,十分优秀!