[Qt-interest] Transparent QWidget on top of a QGLWidget parent

Carl Snellman carl.snellman at gmail.com
Sat Mar 6 19:37:37 CET 2010


Hey,

I have a problem I could not find any solution on the web etc, so I
need to turn to Qt Gods....

The problem is that I'm trying to place a transparent custom widget
into QGLWidget. You would expect (at least a novice like myself :)
that the QGLWIdget's content will show as the background of the custom
widget, but actually my computer's desktop (whatever happened to be on
that spot in screen) shines through?!?

I reduced the problem to following minimal code:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <QtGui/QApplication>
#include <QGLWidget>
#include <QPushButton>
#include <qdebug.h>

class GlWidget : public QGLWidget  {
public:
    GlWidget() {
        setAutoFillBackground(true);
    };

    void paintEvent(QPaintEvent * event) {
        QPainter painter(this);
        painter.setPen(QPen(Qt::blue, 1, Qt::SolidLine, Qt::FlatCap,
Qt::MiterJoin));
        // draw vertical blue lines
        for(int x=0; x<geometry().width(); x+=10) {
            painter.drawLine(x, 0, x, geometry().height());
        }
    };
};


class RegularWidget : public QWidget  {
public:
    RegularWidget(QWidget * parent)
        : QWidget(parent)
    {
            setStyleSheet("background-color:  transparent");

            setAutoFillBackground(true);

//            QPalette pal = palette();
//            pal.setColor(backgroundRole(), Qt::transparent);
//            pal.setColor(foregroundRole(), Qt::white);
//            setPalette(pal);
        };

    void paintEvent(QPaintEvent * event) {
        QPainter painter(this);
        // draw horizontal red lines
        painter.setPen(QPen(Qt::red, 1, Qt::SolidLine, Qt::FlatCap,
Qt::MiterJoin));
        for(int y=0; y<geometry().height(); y+=10) {
            painter.drawLine(0, y, geometry().width(), y);
        }

        // green box
        painter.fillRect(QRectF(30,30,50,50), QBrush(QColor(0, 255, 0)));
        painter.end();
    };
};

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    GlWidget w;
    w.setFixedSize(400,400);

    RegularWidget regular(&w);
    regular.setGeometry(60,60,200,200);

    QPushButton * button = new QPushButton(QString("Testing 1 2 3"), &w);
    button->setStyleSheet("color: black; border: 2px solid #8f8f91;
border-radius: 6px; background-color: transparent; width: 150px;
height: 50px;");

    w.show();
    return a.exec();
}
<<<<<<<<<<<<<<<<<<<<<<<<<<

I have tried pretty much every imaginable combination of
setAutofillbackground, palettes, styles but none worked. Is this a
known issue, and is there any workarounds?
Any help is very much appreciated!

Thanks,
Carl

PS.
My enviroment: Ubuntu 9.10 (docs say that this would not work on Mac)
The reason I use QGLWidget is that it gives a bit better performance
(FPS) on my real app.



More information about the Qt-interest-old mailing list