Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Semi-transparent widget on QOpenGLWidget has incorrect color brightness
QtWS25 Last Chance

Semi-transparent widget on QOpenGLWidget has incorrect color brightness

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5.1qopenglwidgetopengl
13 Posts 3 Posters 5.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MoOr
    wrote on last edited by A Former User
    #1

    Here simple test code:

    //*.h
    
    #ifndef FORM_H
    #define FORM_H
    
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions_3_0>
    
    class Form : public QOpenGLWidget, protected QOpenGLFunctions_3_0
    {
        Q_OBJECT
    
    public:
        explicit Form(QWidget *parent = 0);
        // QOpenGLWidget interface
    protected:
        void initializeGL() Q_DECL_OVERRIDE;
    };
    #endif // FORM_H
    
    // *.cpp
    
    #include "form.h"
    #include <QPushButton>
    
    Form::Form(QWidget *parent) :
        QOpenGLWidget(parent)
    {
        QPushButton *f = new QPushButton("f", this);
        QPushButton *s = new QPushButton("s", this);
        s->move(20,10);
        f->setStyleSheet("background-color: white;");
        s->setStyleSheet("background-color: rgba(255,0,0,160);");
    }
    
    void Form::initializeGL()
    {
        initializeOpenGLFunctions();
        glClearColor(1,1,1,1);
    }
    

    Result is:
    alt text

    The color of whole button should be like in marked circle. How to solve this problem.

    [Edit: Added code tags -- @Wieland]

    M J.HilkJ 2 Replies Last reply
    0
    • M Offline
      M Offline
      MoOr
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • M MoOr

        Here simple test code:

        //*.h
        
        #ifndef FORM_H
        #define FORM_H
        
        #include <QOpenGLWidget>
        #include <QOpenGLFunctions_3_0>
        
        class Form : public QOpenGLWidget, protected QOpenGLFunctions_3_0
        {
            Q_OBJECT
        
        public:
            explicit Form(QWidget *parent = 0);
            // QOpenGLWidget interface
        protected:
            void initializeGL() Q_DECL_OVERRIDE;
        };
        #endif // FORM_H
        
        // *.cpp
        
        #include "form.h"
        #include <QPushButton>
        
        Form::Form(QWidget *parent) :
            QOpenGLWidget(parent)
        {
            QPushButton *f = new QPushButton("f", this);
            QPushButton *s = new QPushButton("s", this);
            s->move(20,10);
            f->setStyleSheet("background-color: white;");
            s->setStyleSheet("background-color: rgba(255,0,0,160);");
        }
        
        void Form::initializeGL()
        {
            initializeOpenGLFunctions();
            glClearColor(1,1,1,1);
        }
        

        Result is:
        alt text

        The color of whole button should be like in marked circle. How to solve this problem.

        [Edit: Added code tags -- @Wieland]

        M Offline
        M Offline
        MoOr
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M MoOr

          Here simple test code:

          //*.h
          
          #ifndef FORM_H
          #define FORM_H
          
          #include <QOpenGLWidget>
          #include <QOpenGLFunctions_3_0>
          
          class Form : public QOpenGLWidget, protected QOpenGLFunctions_3_0
          {
              Q_OBJECT
          
          public:
              explicit Form(QWidget *parent = 0);
              // QOpenGLWidget interface
          protected:
              void initializeGL() Q_DECL_OVERRIDE;
          };
          #endif // FORM_H
          
          // *.cpp
          
          #include "form.h"
          #include <QPushButton>
          
          Form::Form(QWidget *parent) :
              QOpenGLWidget(parent)
          {
              QPushButton *f = new QPushButton("f", this);
              QPushButton *s = new QPushButton("s", this);
              s->move(20,10);
              f->setStyleSheet("background-color: white;");
              s->setStyleSheet("background-color: rgba(255,0,0,160);");
          }
          
          void Form::initializeGL()
          {
              initializeOpenGLFunctions();
              glClearColor(1,1,1,1);
          }
          

          Result is:
          alt text

          The color of whole button should be like in marked circle. How to solve this problem.

          [Edit: Added code tags -- @Wieland]

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @MoOr

          Button 2 has an alpha value of 160, Button 1 ist pure (255,255,255,255) where they overlap the background blends with the other button. Exactly as intended.

          if you want the blended color as the default color for button 2 then change your CSS

          IIRC Blend works like this?

          res.r = dst.r * (1 - src.a) + src.r * src.a
          res.g = dst.g * (1 -src.a) + src.g * src.a
          res.b = dst.b * (1 - src.a) + src.b * src.a
          

          so your target rgba should be (255,95,95,255).


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          M 1 Reply Last reply
          0
          • M Offline
            M Offline
            MoOr
            wrote on last edited by
            #5

            The main problem is that 1 button has non-opaque white background and GL background has non-opaque white color, but button 2 has different color on them. In fact, overlaping 1 button is working properly, so why overlaping GL context gives not correct results?

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @MoOr

              Button 2 has an alpha value of 160, Button 1 ist pure (255,255,255,255) where they overlap the background blends with the other button. Exactly as intended.

              if you want the blended color as the default color for button 2 then change your CSS

              IIRC Blend works like this?

              res.r = dst.r * (1 - src.a) + src.r * src.a
              res.g = dst.g * (1 -src.a) + src.g * src.a
              res.b = dst.b * (1 - src.a) + src.b * src.a
              

              so your target rgba should be (255,95,95,255).

              M Offline
              M Offline
              MoOr
              wrote on last edited by MoOr
              #6

              @J.Hilk Thanks for reply. But i need semi-transparent widget on opengl context and problem is that semi-transparent widgets overlap opengl with wrong color(it is darker than should be), in the sample higher i just show that widget-widget overlaping works properly, but widget-glwidget not. More than that if you draw in paintGL rect with rgba(1,0,0,0.62) 0.62 = 160 / 255 and turn on blend with blending SRC_ALPHA, ONE_MINUS_SRC_ALPHA you will see proper color. Try yourself and I will be grateful if you told me how to fix this.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MoOr
                wrote on last edited by MoOr
                #7

                What a strange silence, to clarify the issue with images

                Now:
                alt text
                should be:
                alt text

                D 1 Reply Last reply
                0
                • M MoOr

                  What a strange silence, to clarify the issue with images

                  Now:
                  alt text
                  should be:
                  alt text

                  D Offline
                  D Offline
                  Devopia53
                  wrote on last edited by Devopia53
                  #8

                  @MoOr

                  Hi.

                  I tested your source code on Qt5.8/Qt5.7(with Win7/mingw or Win10/MSVC2015), but I can't find any problems.

                  Result

                  My opinion is to upgrade Qt or upgrade your graphics card driver.

                  M 1 Reply Last reply
                  0
                  • D Devopia53

                    @MoOr

                    Hi.

                    I tested your source code on Qt5.8/Qt5.7(with Win7/mingw or Win10/MSVC2015), but I can't find any problems.

                    Result

                    My opinion is to upgrade Qt or upgrade your graphics card driver.

                    M Offline
                    M Offline
                    MoOr
                    wrote on last edited by MoOr
                    #9

                    @Devopia53 Hi! thanks for replay! I have tested on 5.7.0/mingw/msvc on two machines(one has integrated card, second has video card nvidia), but on both incorrect results, I am so sad. Is yours 'main.cpp' looks like this:

                    #include <QApplication>
                    #include "form.h"
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        Form f;
                        f.show();
                    
                        return a.exec();
                    }
                    
                    D 1 Reply Last reply
                    0
                    • M MoOr

                      @Devopia53 Hi! thanks for replay! I have tested on 5.7.0/mingw/msvc on two machines(one has integrated card, second has video card nvidia), but on both incorrect results, I am so sad. Is yours 'main.cpp' looks like this:

                      #include <QApplication>
                      #include "form.h"
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          Form f;
                          f.show();
                      
                          return a.exec();
                      }
                      
                      D Offline
                      D Offline
                      Devopia53
                      wrote on last edited by Devopia53
                      #10

                      @MoOr

                      Don't be sad! Please! :)

                      I tested it exactly the same as your code and my OpenGL driver version is 3.3(to upgrade 4.0 is ok).
                      Please check the version of OpenGL driver using the following program.
                      Here

                      M 1 Reply Last reply
                      0
                      • D Devopia53

                        @MoOr

                        Don't be sad! Please! :)

                        I tested it exactly the same as your code and my OpenGL driver version is 3.3(to upgrade 4.0 is ok).
                        Please check the version of OpenGL driver using the following program.
                        Here

                        M Offline
                        M Offline
                        MoOr
                        wrote on last edited by MoOr
                        #11

                        @Devopia53 alt text
                        Result incorrect. :-( Can you print your QSurfaceFormat(qDebug() << form.context().format()) to compare with mine
                        my debug is:

                        QSurfaceFormat(version 4.3, options QFlags(0x4), depthBufferSize 24, redBufferSize 8, greenBufferSize 8, blueBufferSize 8, alphaBufferSize 8, stencilBufferSize 8, samples 0, swapBehavior 2, swapInterval 1, profile 2)

                        D 1 Reply Last reply
                        0
                        • M MoOr

                          @Devopia53 alt text
                          Result incorrect. :-( Can you print your QSurfaceFormat(qDebug() << form.context().format()) to compare with mine
                          my debug is:

                          QSurfaceFormat(version 4.3, options QFlags(0x4), depthBufferSize 24, redBufferSize 8, greenBufferSize 8, blueBufferSize 8, alphaBufferSize 8, stencilBufferSize 8, samples 0, swapBehavior 2, swapInterval 1, profile 2)

                          D Offline
                          D Offline
                          Devopia53
                          wrote on last edited by Devopia53
                          #12

                          @MoOr

                          http://imgur.com/a/ANxwq

                          QSurfaceFormat(version 3.3, options QFlags(0x4), depthBufferSize 24, redBufferSize 8, greenBufferSize 8, blueBufferSize 8, alphaBufferSize 8, stencilBufferSize 8, samples 0, swapBehavior 2, swapInterval 1, profile 2)

                          M 1 Reply Last reply
                          0
                          • D Devopia53

                            @MoOr

                            http://imgur.com/a/ANxwq

                            QSurfaceFormat(version 3.3, options QFlags(0x4), depthBufferSize 24, redBufferSize 8, greenBufferSize 8, blueBufferSize 8, alphaBufferSize 8, stencilBufferSize 8, samples 0, swapBehavior 2, swapInterval 1, profile 2)

                            M Offline
                            M Offline
                            MoOr
                            wrote on last edited by MoOr
                            #13

                            @Devopia53 I tested on Qt 5.8/Win7 today. The same incorrect result. But today i discovered the difference in overlaping behavior on my two machines. On the machine with video card fully-transparent widget has black color on white opengl context :D, where as other machine with integrated video card has no color(as should). So the gui transparency on opengl context is very platform dependent. Solution is to render widget by opengl directly but this way is very inconvenient. I think troll-tech must solve this problem in future.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved