Qt World Summit: Register Today!

QGraphicsView (OpenGL Viewport) issue with Stylesheet

  • Hi I'm developing a graphical Qt application under windows I used QGraphicsView within a QOpenGLwidget for it's viewport the problem is when I don't use any Stylsheet it works well but if I use stylsheet in any parent of QGraphicsView the background doesn't refresh (repaint) anymore and if I draw a graphicsitem it doesn't remove until I resize the QGraphicsView! what do you think ? is it a bug in Qt 5.9.2 ? or there is a solution ? of course I need to use stylesheet in my app.

    here is source code for the GraphicsView ...

    #ifdef USE_OpenGL
        QSurfaceFormat format = QSurfaceFormat::defaultFormat();
    ui->gV->setRenderHints(QPainter::Antialiasing| QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform| QPainter::TextAntialiasing);
    ui->gV->setVerticalScrollBarPolicy  (Qt::ScrollBarAlwaysOff);
    //ui->gV->setAttribute(Qt::WA_OpaquePaintEvent, true);
    //ui->gV->setAttribute(Qt::WA_NoSystemBackground, true);

    also if I don't use OpenGL view port it again works and if I set cacheMode to QGraphicsView::CacheBackground the it works again but it doesn't use OpenGL anymore !

    thank you for your kind answers

    image of app

  • Lifetime Qt Champion

    Well what do you use in the stylesheet?
    If you exclude QGraphicsView and QOpenGLwidget does it still happen?

  • Hi
    I don't have any style for QGraphicsView and QOpenGLwidget in my stylesheet, if I only set background-color for any parent widget of the QGraphicsView widget it happen and I have background color for QWidget class so all other widgets including QGraphicsView and QOpenGLwidget inherit from it.

  • Lifetime Qt Champion

    As far as i know QOpenGLwidget do not use QStyle at all so
    its not unexpected that stylesheet do not work on/with it.
    So its not supported I guess.

  • Hi

    so what is the solution as I need to use the stylesheets for all other widgets ?

  • Lifetime Qt Champion


    Well, use names, types or or any of the specifiers. instead of the "hit them all"
    QWidget type


    Maybe there is also something CSS2 selector syntax:

    Not sure that "not" specifier is supported.

    but there is always
    syntax and you could just add that to the QGraphicsView and some property so it
    wont be affected but all others does.

  • @mrjj

    OK very good solution I changed all the widget styles to QWidget[acceptDrops=false] and set acceptDrops to true in QGraphicsView and its parent so the style doesn't affect on it but there is another problem ,I have a frame which contains the QGraphicsView and I need to style the frame by background-color key, but when I do so, the The problem goes back again!

    how can I set style to the frame without affecting the QGraphicsView ?

    the stye is

    background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, y2:0, stop:0 rgba(15, 21, 27, 255), stop:1 rgba(67, 74, 86, 255));
    border-radius: 15px;
    border-width : 0px;

  • @hamed_ta

    Oh I found it, I set the stylesheet of the Qframe to

    	background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, y2:0, stop:0 rgba(15, 21, 27, 255), stop:1 rgba(67, 74, 86, 255));
    	border-radius: 15px;
    	border-width : 0px;

    thank you so much

  • Lifetime Qt Champion

    Super :)
    Just as a note , you can add custom (dynamic) properties with the green + over the property list but
    if you never need to use drag and drop that property is just as good.

  • @mrjj


Log in to reply