Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, January 2007
[Qt 4.2.1] QPainter, QWidget, QGLWidget and transparency


Message 1 in thread

Hi All!

I've met a trouble with translucent widgets in Qt4. I have the following code:
TestChildWidget.hpp

#ifndef __TEST_CHILD_WIDGET_HPP
#define __TEST_CHILD_WIDGET_HPP

#include <QWidget>

class TestChildWidget : public QWidget
{
    Q_OBJECT
public:
    TestChildWidget(QWidget * parent = NULL);
    ~TestChildWidget() {};
};

#endif

TestChildWidget.cpp

#include <TestChildWidget.hpp>
#include <QPalette>

TestChildWidget::TestChildWidget(QWidget * parent/* = NULL*/)
   : QWidget(parent)
{
    QPalette pal(palette());
    pal.setColor(QPalette::Window, QColor(255, 0, 0, 120));
    setPalette(pal);
    setAutoFillBackground(true);
};

TestWidget.hpp

#ifndef __TEST_WIDGET_HPP
#define __TEST_WIDGET_HPP

#include <QWidget>

class QPaintEvent;
class TestWidget : public QWidget
{
    Q_OBJECT

public:
    TestWidget(QWidget * parent = NULL);
    ~TestWidget() {};

protected:
    void paintEvent(QPaintEvent * event);

};

#endif

TestWidget.cpp

#include <TestWidget.hpp>
#include <QPainter>
#include <QPaintEvent>
#include <QPalette>

TestWidget::TestWidget(QWidget * parent/* = NULL*/)
    : QWidget(parent)
{

    QPalette pal(palette());
    pal.setColor(QPalette::Window, QColor(255, 255, 255, 255));
    setPalette(pal);
    setAutoFillBackground(true);
};

void TestWidget::paintEvent(QPaintEvent * event)
{
    QPainter painter;
    painter.begin(this);
    painter.setRenderHint(QPainter::TextAntialiasing);
    painter.drawText(30, 30, "TEST STRING!!!!");
    painter.end();
};

main.cpp

#include <TestWidget.hpp>
#include <TestChildWidget.hpp>
#include <QApplication>
int main(int argc, char ** argv)
{
    QApplication app(argc, argv);
    TestWidget * parent = new TestWidget();
    TestChildWidget * child = new TestChildWidget(parent);
    TestChildWidget * child2 = new TestChildWidget(parent);
    parent->resize(200, 100);
    child->resize(50, 50);
    child2->resize(50, 50);
    child->move(10, 10);
    child2->move(20, 20);
    parent->show();
    return app.exec();
};

This code works well: it creates the window, outputs the test string to it and 
paints to translucent widgets over it. Everything looks nice. But when I 
tried to change base class of TestWidget and TestChildWidget from QWidget to 
QGLWidget (so i try to use OpenGL to speed up the rendering) i receive bad 
result. There is a window, but instead of translucent widgets i get two black 
rectangles. 

Does anybody have any ideas to solve this issue?...

WBR, Gleb.

PS. Sorry for not-so-well English...

-- 
 [ signature omitted ] 

Message 2 in thread

Hi,

Sectoid wrote:
> Hi All!
> 
> I've met a trouble with translucent widgets in Qt4. I have the following code:
> TestChildWidget.hpp

[snip]

> This code works well: it creates the window, outputs the test string to it and 
> paints to translucent widgets over it. Everything looks nice. But when I 
> tried to change base class of TestWidget and TestChildWidget from QWidget to 
> QGLWidget (so i try to use OpenGL to speed up the rendering) i receive bad 
> result. There is a window, but instead of translucent widgets i get two black 
> rectangles. 
> 
> Does anybody have any ideas to solve this issue?...
> 
> WBR, Gleb.
> 
> PS. Sorry for not-so-well English...

QGLWidgets are always considered to be opaque internally in Qt, and 
can't have a semi-transparent background because of several reasons. In 
short, the QGLWidgets do not participate in the compositing process that 
takes place inside the internal Qt backingstore, which we use for 
blending semi-transparent child widgets.

--
 [ signature omitted ]