Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-137162

QOpenGLWidget, QPainter and QScrollArea do not fit together

    XMLWordPrintable

Details

    • Linux/Wayland, Windows

    Description

      QOpenGLWidget, QPainter and QScrollArea do not fit together:

      #include <QApplication>
      #include <QMainWindow>
      #include <QOpenGLFunctions_2_1>
      #include <QOpenGLWidget>
      #include <QPainter>
      #include <QScrollArea>
      
      class GLWidget final : public QOpenGLWidget, public QOpenGLFunctions_2_1 {
      
        auto initializeGL() -> void override {
          initializeOpenGLFunctions();
        }
      
        auto paintGL() -> void override {
          glClearColor(0.0, 0.0, 0.0, 1.0);
          glClear(GL_COLOR_BUFFER_BIT);
          glBegin(GL_TRIANGLES);
          glColor3d(1.0, 0.0, 0.0);
          glVertex3d(0.0, 1.0, 0.0);
          glColor3d(0.0, 1.0, 0.0);
          glVertex3d(-1.0, -1.0, 0.0);
          glColor3d(0.0, 0.0, 1.0);
          glVertex3d(1.0, -1.0, 0.0);
          glEnd();
          auto painter = QPainter{};
          painter.begin(this);
          painter.setPen(QColor{"yellow"});
          painter.drawText(100, 100, "Text");
          painter.end();
        }
      };
      
      auto main(int argc, char** argv) -> int {
        auto app = QApplication{argc, argv};
        auto win = QMainWindow{};
        auto widget = new GLWidget{};
        widget->setMinimumSize(QSize{250, 250});
        auto scrollArea = new QScrollArea{};
        scrollArea->setWidget(widget);
        win.setCentralWidget(scrollArea);
        //win.setCentralWidget(widget);
        win.resize(500, 500);
        win.show();
        return QApplication::exec();
      }
      
      • When running on Windows, the triangle is not shown.
      • Running on Linux with QT_QPA_PLATFORMTHEME=qt6ct , the triangle is not shown.
      • Running on Linux with KDE-Theme the triangle is shown.
      • After removing QScrollArea the triangle is shown.

      So something seems to be completely wrong either with my sample program or with Qt!

      When using only QPainter everything is fine:

      #include <QApplication>
      #include <QMainWindow>
      #include <QOpenGLFunctions_2_1>
      #include <QOpenGLWidget>
      #include <QPainter>
      #include <QScrollArea>
      
      class GLWidget final : public QOpenGLWidget, public QOpenGLFunctions_2_1 {
      
        auto initializeGL() -> void override {
          initializeOpenGLFunctions();
        }
      
        auto paintGL() -> void override {
          auto painter = QPainter{this};
          painter.fillRect(0, 0, 250, 250, QColor{"black"});
          painter.setPen(QColor{"red"});
          painter.drawLine(QPoint{125, 10}, QPoint{10, 240});
          painter.drawLine(QPoint{10, 240}, QPoint{240, 240});
          painter.drawLine(QPoint{240, 240}, QPoint{125, 10});
          painter.setPen(QColor{"yellow"});
          painter.drawText(100, 100, "Text");
        }
      };
      
      auto main(int argc, char** argv) -> int {
        auto app = QApplication{argc, argv};
        auto win = QMainWindow{};
        auto widget = new GLWidget{};
        widget->setMinimumSize(QSize{250, 250});
        auto scrollArea = new QScrollArea{};
        scrollArea->setWidget(widget);
        win.setCentralWidget(scrollArea);
        //win.setCentralWidget(widget);
        win.resize(500, 500);
        win.show();
        return QApplication::exec();
      }
      

      When separating QOpenGLWidget and QPainter everything is fine:

      #include <QApplication>
      #include <QMainWindow>
      #include <QOpenGLFunctions_2_1>
      #include <QOpenGLWidget>
      #include <QPainter>
      #include <QScrollArea>
      #include <QStackedLayout>
      
      class GLWidget final : public QOpenGLWidget, public QOpenGLFunctions_2_1 {
      
        auto initializeGL() -> void override {
          initializeOpenGLFunctions();
        }
      
        auto paintGL() -> void override {
          glClearColor(0.0, 0.0, 0.0, 1.0);
          glClear(GL_COLOR_BUFFER_BIT);
          glBegin(GL_TRIANGLES);
          glColor3d(1.0, 0.0, 0.0);
          glVertex3d(0.0, 1.0, 0.0);
          glColor3d(0.0, 1.0, 0.0);
          glVertex3d(-1.0, -1.0, 0.0);
          glColor3d(0.0, 0.0, 1.0);
          glVertex3d(1.0, -1.0, 0.0);
          glEnd();
        }
      };
      
      class TextWidget final : public QWidget {
        auto paintEvent(QPaintEvent*) -> void override {
          auto painter = QPainter{};
          painter.begin(this);
          painter.setPen(QColor{"yellow"});
          painter.drawText(100, 100, "Text");
          painter.end();
        }
      };
      
      auto main(int argc, char** argv) -> int {
        auto app = QApplication{argc, argv};
        auto win = QMainWindow{};
        auto glWidget = new GLWidget{};
        auto textWidget = new TextWidget{};
        auto stackedLayout = new QStackedLayout{};
        stackedLayout->setStackingMode(QStackedLayout::StackAll);
        stackedLayout->addWidget(glWidget);
        stackedLayout->addWidget(textWidget);
        auto widget = new QWidget{};
        widget->setLayout(stackedLayout);
        widget->setMinimumSize(QSize{250, 250});
        auto scrollArea = new QScrollArea{};
        scrollArea->setWidget(widget);
        win.setCentralWidget(scrollArea);
        //win.setCentralWidget(widget);
        win.resize(500, 500);
        win.show();
        return QApplication::exec();
      }
      

      According to the QOpenGLWidget documentation direct OpenGL commands can be used together with QPainter. This does not seem the case in presence of a QScrollArea. Using beginNativePainting()/endNativePainting() does not help.

       

      Attached are Zips containing everything needed to reproduce.

      Attachments

        1. glwidget-painter-only.zip
          1 kB
          Wolfgang Seifert
        2. glwidget-painter-opengl.zip
          1 kB
          Wolfgang Seifert
        3. glwidget-stacked-painter-opengl.zip
          1 kB
          Wolfgang Seifert
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            lagocs Laszlo Agocs
            wolf.seifert Wolfgang Seifert
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes