Qt OpenGL background transparency



  • Hello!
    I'm asking a new question here. I've goggled "qt opengl background transparency" and found almost nothing. I would like to have that.

    This is what i've found:

    "
    I think it's not possible to set the background transparent (becase on every
    repaint of an opengl scene (the whole widget) you have to clear the whole
    color buffer).

    But you can try one of those:

    • grabbing the widgets behind the QGLWidget and use that pixmap as a
      background texture for the QGLWidget

    • render the QGLWidget on a pbuffer (see the related qt class), then take the
      resulting pixmap and paint it on the top of your window

    • use the QGraphicsView framework over a QGLWidget. This way you can mix
      QGraphicsItems (drawn by QPainter) and native OpenGL calls [* best results]

    Of course other ways to do that may exist, but that's what I can think about
    right now. If you can provide more details on your application we can try to
    give you better hints!

    Ciao,
    Enrico
    "
    I digged more about "QGraphicsView framework over a QGLWidget."
    I've wrote a test project:
    !http://img42.imageshack.us/img42/7149/screenshotat20111216141.png(output)!

    main.cpp:
    @#include <QtGui>

    #include "mainwindow.h"
    #include "glgraphicsview.h"
    #include "glgraphicsscene.h"
    #include "myglwidget.h"

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

    QApplication *app = new QApplication( argc , argv );
    
    MainWindow *w = new MainWindow;
    
    GLGraphicsView *view = new GLGraphicsView;
    
    GLGraphicsScene *scene = new GLGraphicsScene;
    
    MyGLWidget *glWidget = new MyGLWidget;
    
    view->setViewport( glWidget );
    view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    view->setScene( scene );
    view->setStyleSheet("background: transparent;border:none;");
    //view->setViewport( new QGLWidget() );
    
    w->setCentralWidget( view );
    w->setWindowTitle( "Transparent OpenGl background" );
    w->resize(320,240);
    w->show();
    
    return app->exec&#40;&#41;;
    

    }
    @

    GLGraphicsScene.cpp:
    @#include "glgraphicsscene.h"

    GLGraphicsScene::GLGraphicsScene(){
    }

    void GLGraphicsScene::drawBackground(QPainter* painter, const QRectF& rect){

    Q_UNUSED(rect)
    
    if( (painter->paintEngine()->type() != QPaintEngine::OpenGL2) &&
        (painter->paintEngine()->type() != QPaintEngine::OpenGL))
    {
        qDebug()<<"GLGraphicsScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view";
        return;
    }
    //qDebug()<<"GLGraphicsScene: paint. ok.";
    
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    glBegin(GL_TRIANGLES);
    
        glColor3f( 1 , 0 , 0 );
        glVertex3f( -0.5f, -0.5f, 0.0f);
    
        glColor3f( 0 , 1 , 0 );
        glVertex3f( 0.5f, -0.5f, 0.0f);
    
        glColor3f( 0 , 0 , 1 );
        glVertex3f( 0.0f, 0.5f, 0.0f);
    
    glEnd();
    

    }
    @

    GLGraphicsView.cpp:
    @#include "glgraphicsview.h"

    GLGraphicsView::GLGraphicsView( QWidget *parent )
    : QGraphicsView( parent ){

    }
    void GLGraphicsView::resizeEvent(QResizeEvent *event) {

    if ( scene() ){
        scene()->setSceneRect( QRect( QPoint(0,0),event->size() ) );
    }
    
    QGraphicsView::resizeEvent(event);
    

    }
    @

    MainWindow.cpp:
    @#include "mainwindow.h"
    #include <QPainter>

    MainWindow::MainWindow( QWidget *parent )
    : QMainWindow( parent ){

    setAttribute(Qt::WA_TranslucentBackground);
    

    }

    void MainWindow::paintEvent(QPaintEvent *pe){
    QMainWindow::paintEvent( pe );

    QPainter p( this );
    p.setOpacity( 0.5 );
    p.setBrush( QColor( "#347daf" ) );//little bluish
    p.setPen( Qt::NoPen );
    p.setRenderHint( QPainter::Antialiasing );
    p.drawRect( 0.0 , 0.0 , this->width() , this->height() );
    

    }

    @

    MyGLWidget.cpp:
    @#include "myglwidget.h"

    MyGLWidget::MyGLWidget( QWidget *parent )
    : QGLWidget( parent ){
    }
    @

    GLGraphicsScene.h:
    @#ifndef GLGRAPHICSSCENE_H
    #define GLGRAPHICSSCENE_H

    #include <QGraphicsScene>
    #include <QDebug>
    #include <QtOpenGL>
    #include <QPainter>
    #include <QRectF>

    class GLGraphicsScene : public QGraphicsScene{

    public:
        GLGraphicsScene();
        void drawBackground(QPainter* painter, const QRectF& rect);
    

    };

    #endif // GLGRAPHICSSCENE_H
    @

    GLGraphicsView.h:
    @#ifndef GLGRAPHICSVIEW_H
    #define GLGRAPHICSVIEW_H

    #include <QGraphicsView>
    #include <QResizeEvent>

    class GLGraphicsView : public QGraphicsView{

    Q_OBJECT
    
    public:
        GLGraphicsView( QWidget *parent = 0 );
    protected:
        void resizeEvent( QResizeEvent* );
    

    };

    #endif // GLGRAPHICSVIEW_H
    @

    MainWindow.h:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    class MainWindow : public QMainWindow{
    Q_OBJECT
    public:
    MainWindow( QWidget *parent = 0 );
    protected:
    void paintEvent(QPaintEvent *pe);
    };

    #endif //MAINWINDOW_H
    @

    MyGLWidget.h:
    @#ifndef MYGLWIDGET_H
    #define MYGLWIDGET_H

    #include <QGLWidget>

    class MyGLWidget : public QGLWidget{

    Q_OBJECT
    
    public:
        explicit MyGLWidget( QWidget *parent = 0 );
    

    };

    #endif // MYGLWIDGET_H
    @

    untitled2.pro
    @QT += core gui opengl

    SOURCES +=
    main.cpp
    mainwindow.cpp
    glgraphicsscene.cpp
    glgraphicsview.cpp
    myglwidget.cpp

    HEADERS +=
    mainwindow.h
    glgraphicsscene.h
    glgraphicsview.h
    myglwidget.h
    @

    And no transparency..... How should i modify this code?



  • Generally OpenGL windows (namely QGLWidget) don’t support window transparency.

    I’ve seen solutions that use offscreen OpenGL rendering with bliting into a transparent window, or enabling a platform specific extension. I haven’t seen a solution that is implementable with the Qt APIs or one that is generally cross-platform. If someone knows one, I’d love to hear about it.



  • One thing I can add here:
    You need to:
    setAttribute(Qt::WA_TranslucentBackground);

    on both the Viewport (QGLWidget) AND the QGraphicsView

    On my linux machine with a compositing manger running I can have opengl-painted qml applications with "holes" :)

    Now I add a questions:
    This is not working on windows (with running aero) ... anyone an idea why?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.