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. Accelerate QGraphicsView with openGL widget - correct up to date workflow?
Forum Updated to NodeBB v4.3 + New Features

Accelerate QGraphicsView with openGL widget - correct up to date workflow?

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 2 Posters 8.3k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #5

    Hmm interesting, testing it as I go.

    So I use this to stylemy app in main >

            f.open(QFile::ReadOnly | QFile::Text);
            QTextStream ts(&f);
            qApp->setStyleSheet(ts.readAll());
    

    But that appears to be causing an issue with the glWidget with stylesheets as there is ghosting of items.

    And here is a weird problem.

    I subclass qOpenGLWidget and reimplemented initialize& paint to paint background as >

    void icGlWidget::initializeGL() {
        QOpenGLFunctions::initializeOpenGLFunctions();
        glClearColor(.5, .5, .5, 1);
    }
    
    void icGlWidget::resizeGL(int w, int h) {
        QOpenGLWidget::resizeGL(w, h);
    }
    
    void icGlWidget::paintGL() {
        glClearColor(.5, .5, .5, 1);
    }
    

    And here is the weird stuff.

    If I apply styleSheet ,the glClearColor() works and sets the color of the background however, that produce the ghosting of items. However, if I remove the styleSheet, then the glClearColor no longer works and I get black background... so... the question is...

    How do I apply style sheet, but not cause ghosting, but are able to change background color / draw grid? :- )

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dariusz
      wrote on last edited by Dariusz
      #6

      Attached image showing the issue. This is using drawBackground in qgraphicsScene + stylesheet. I like style because it sets all colors of my app but the GL widget seems to have big problem with it :- ( www.dariuszmakowski.com/ghost.jpg

      K 1 Reply Last reply
      0
      • D Dariusz

        Attached image showing the issue. This is using drawBackground in qgraphicsScene + stylesheet. I like style because it sets all colors of my app but the GL widget seems to have big problem with it :- ( www.dariuszmakowski.com/ghost.jpg

        K Offline
        K Offline
        kenchan
        wrote on last edited by
        #7

        @Dariusz

        The things that get drawn with OpenGL are your QGraphicsItems in their paint() functions.
        inside calls to painter->beginNativePainting(); and painter->endNativePainting();. Anything other than that gets draw by the normal painter.
        something like this should work. Other than this I don't know of any other way to accelerate drawing in a QGraphicsScene in a QGraphicsView.

        void MyGraphicItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
        {
            if( qobject_cast<QOpenGLWidget*>(widget) != 0 )
            {
                painter->beginNativePainting();
                 ...
                your OpenGL calls go here
                ...
                painter->endNativePainting();
            }
            else
           {
              ...
              normal painter code could go here
              ...
           }
        }
        

        I do not know anything about how styles work with this setup because I have never used them in this context.
        You are getting a QStyleOptionGraphicsItem object passed in, maybe you need to look at what that can do for you.

        K 1 Reply Last reply
        1
        • D Offline
          D Offline
          Dariusz
          wrote on last edited by
          #8

          Hmmm fairly lost... I made a standalone one file example test, can you/any have a look in to it? I'm lost with the beginNativePainting it seems to not do anything if I use it everywhere or not... :- (

          main.cpp

          #include <iostream>
          #include "QApplication"
          #include "QGraphicsView"
          #include "QGraphicsScene"
          #include "QOpenGLWidget"
          #include "QOpenGLFunctions"
          #include "QGraphicsItem"
          #include "QPainter"
          #include "QStyleOptionGraphicsItem"
          #include "qdebug.h"
          
          
          class myItem : public QGraphicsItem {
          public:
              myItem(int x, int y, int width, int height);
          
              QRectF boundingRect() const;
          
              void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
          
          private:
              QRect mRect;
          };
          
          QRectF myItem::boundingRect() const {
              return mRect;
          }
          
          void myItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
          #
          
          
              if (qobject_cast<QOpenGLWidget *>(widget) != 0) {
                  painter->beginNativePainting();
                  qDebug() << "im gl widget? - how do I paint it here?";
                  painter->endNativePainting();
              } else {
              }
          
              qDebug() << "im non gl widget?";
          
              painter->drawText(0, -5, "test");
          
              painter->setBrush(QBrush(QColor(150, 150, 150, 255)));
              painter->drawRoundedRect(mRect, 5, 5);
              painter->setBrush(QBrush(QColor(75, 75, 75)));
              painter->drawRoundedRect(QRect(mRect.x() + 5, mRect.y() + 5, mRect.width() - 10, mRect.height() - 10), 2, 2);
          
              if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus)) {
                  //qt_graphicsItem_highlightSelected(this, painter, option);
                  //// Write selection look here ?
              }
          }
          
          
          myItem::myItem(int x, int y, int width, int height) {
              mRect = QRect(x, y, width, height);
          }
          
          
          class gGLView : public QOpenGLWidget, public QOpenGLFunctions {
          
          public:
              gGLView();
          
          protected:
              void initializeGL();
          
              void resizeGL(int w, int h);
          
              void paintGL();
          
          };
          
          gGLView::gGLView() {
              QSurfaceFormat format;
              format.setRenderableType(QSurfaceFormat::OpenGL);
              format.setProfile(QSurfaceFormat::CoreProfile);
              format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
              //format.setVersion(4, 1);
              format.setSamples(16);
              setFormat(format);
          }
          
          void gGLView::initializeGL() {
              QOpenGLFunctions::initializeOpenGLFunctions();
              glClearColor(.5, .1, .8, 1);
          }
          
          void gGLView::resizeGL(int w, int h) {
              QOpenGLWidget::resizeGL(w, h);
          }
          
          void gGLView::paintGL() {
              glClearColor(.5, .2, .7, 1);
          }
          
          
          class gScene : public QGraphicsScene {
          
          public:
              gScene();
          
              void drawBackground(QPainter *painter, const QRectF &rect);
          
          };
          
          gScene::gScene() {
          
          }
          
          void gScene::drawBackground(QPainter *painter, const QRectF &rect) {
              //QGraphicsScene::drawBackground(painter, rect);
          
              const int gridSize = 20;
          
          
              qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
              qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
          
              QVarLengthArray<QLineF, 100> lines;
          
              for (qreal x = left; x < rect.right(); x += gridSize)
                  lines.append(QLineF(x, rect.top(), x, rect.bottom()));
              for (qreal y = top; y < rect.bottom(); y += gridSize)
                  lines.append(QLineF(rect.left(), y, rect.right(), y));
          
              QPen penHLines(QColor(75, 75, 75), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin);
              painter->setPen(penHLines);
              painter->drawLines(lines.data(), lines.size());
          
          }
          
          class gView : public QGraphicsView {
          
          public:
              gView();
          
          private:
              gGLView *mGLView;
              gScene *mScene;
          };
          
          gView::gView() {
          
              mGLView = new gGLView();//QGLFormat(QGL::SampleBuffers));
              setViewport(mGLView);
          
              mScene = new gScene();
              setScene(mScene);
          
              myItem *testItem = new myItem(0, 0, 200, 200);
              scene()->addItem(testItem);
              testItem->setFlag(QGraphicsItem::ItemIsSelectable);
              testItem->setFlag(QGraphicsItem::ItemIsMovable);
              testItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
          }
          
          
          int main(int argc, char **argv) {
              QApplication app(argc, argv);
              qApp->setStyleSheet("QWidget { background-color: yellow }");
          
              std::cout << "Hello, World!" << std::endl;
              gView view2;
              view2.show();
              view2.setGeometry(150, 150, 700, 700);
          
              return app.exec();
          }
          

          cmake

          cmake_minimum_required(VERSION 3.10)
          project(QOpenGL_GraphicsView_01)
          
          set(CMAKE_CXX_STANDARD 17)
          set(CMAKE_CXX_EXTENSIONS OFF)
          
          set(CMAKE_CXX_STANDARD_REQUIRED ON)
          
          set(CMAKE_INCLUDE_CURRENT_DIR ON)
          set(CMAKE_AUTOMOC ON)
          set(CMAKE_AUTOUIC ON)
          
          set(CMAKE_AUTORCC ON)
          set(CMAKE_USE_QTGUI TRUE)
          set(CMAKE_USE_QTXML TRUE)
          
          if(MSVC_VERSION GREATER_EQUAL "1900")
              include(CheckCXXCompilerFlag)
              CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
              message("VS YAY LATEST")
              if(_cpp_latest_flag_supported)
                  message("Added17 flag")
                  add_compile_options("/std:c++17")
              endif()
          
          endif()
          
          find_package(OpenMP)
          find_package(OpenGL REQUIRED)
          
          if(OPENMP_FOUND)
              set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
              set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
              message("Got openMP!")
          endif()
          
          
          find_package(Qt5Widgets)
          find_package(Qt5Core)
          find_package(Qt5OpenGL)
          
          find_package(Qt5Gui )
          find_package(Qt5Network )
          find_package(Qt5Qml )
          find_package(Qt5Quick )
          find_package(Qt5Multimedia )
          find_package(Qt5MultimediaWidgets )
          
          find_package(OpenGL)
          
          
          add_executable(${PROJECT_NAME} main.cpp )
          
          
          
          
          qt5_use_modules(${PROJECT_NAME} Core Widgets Gui Network Qml Quick Multimedia Multimedia Concurrent OpenGL)
          target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Widgets ${QT_LIBRARIES} ${OPENGL_LIBRARIES} Qt5::OpenGL Qt5::Gui Qt5::Network Qt5::Qml Qt5::Quick Qt5::Multimedia Qt5::MultimediaWidgets Qt5::Concurrent opengl32.lib)
          

          TIA !

          1 Reply Last reply
          0
          • K kenchan

            @Dariusz

            The things that get drawn with OpenGL are your QGraphicsItems in their paint() functions.
            inside calls to painter->beginNativePainting(); and painter->endNativePainting();. Anything other than that gets draw by the normal painter.
            something like this should work. Other than this I don't know of any other way to accelerate drawing in a QGraphicsScene in a QGraphicsView.

            void MyGraphicItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
            {
                if( qobject_cast<QOpenGLWidget*>(widget) != 0 )
                {
                    painter->beginNativePainting();
                     ...
                    your OpenGL calls go here
                    ...
                    painter->endNativePainting();
                }
                else
               {
                  ...
                  normal painter code could go here
                  ...
               }
            }
            

            I do not know anything about how styles work with this setup because I have never used them in this context.
            You are getting a QStyleOptionGraphicsItem object passed in, maybe you need to look at what that can do for you.

            K Offline
            K Offline
            kenchan
            wrote on last edited by kenchan
            #9

            @kenchan
            Hey, just to add to what i just said...

            There is another way to accelerate drawing stuff in a QGraphicsView. If you derive your own class and implement the drawBackground() and drawForeground() functions.
            You still need to use the beginNativePainting() and endNativePainting() functions and separate OpenGL call from normal Painter calls.
            I use this technique to draw grids and other stuff using OpenGL. Notice I am saving and restoring the painter state, this is important.
            Something like this...

            void MyGraphicsView::drawBackground(QPainter * painter, const QRectF & rect)
            {
                if(painter->paintEngine()->type() == QPaintEngine::OpenGL2) <<< or whatever OpenGL spec is good for you
                {
                    painter->save();
                    painter->beginNativePainting();
                    ...
                      do your super OPenGL stuff here
                    ...
                    painter->endNativePainting();
                    painter->restore();
                }
                else
                {
                    QGraphicsView::drawBackground(painter,rect);
                }
            }
            
            D 1 Reply Last reply
            1
            • K kenchan

              @kenchan
              Hey, just to add to what i just said...

              There is another way to accelerate drawing stuff in a QGraphicsView. If you derive your own class and implement the drawBackground() and drawForeground() functions.
              You still need to use the beginNativePainting() and endNativePainting() functions and separate OpenGL call from normal Painter calls.
              I use this technique to draw grids and other stuff using OpenGL. Notice I am saving and restoring the painter state, this is important.
              Something like this...

              void MyGraphicsView::drawBackground(QPainter * painter, const QRectF & rect)
              {
                  if(painter->paintEngine()->type() == QPaintEngine::OpenGL2) <<< or whatever OpenGL spec is good for you
                  {
                      painter->save();
                      painter->beginNativePainting();
                      ...
                        do your super OPenGL stuff here
                      ...
                      painter->endNativePainting();
                      painter->restore();
                  }
                  else
                  {
                      QGraphicsView::drawBackground(painter,rect);
                  }
              }
              
              D Offline
              D Offline
              Dariusz
              wrote on last edited by
              #10

              @kenchan said in Accelerate QGraphicsView with openGL widget - correct up to date workflow?:

              Interesting thanks! I just added a one file example above your post. Looks like we posted at the same time :- )))

              K 2 Replies Last reply
              0
              • D Dariusz

                @kenchan said in Accelerate QGraphicsView with openGL widget - correct up to date workflow?:

                Interesting thanks! I just added a one file example above your post. Looks like we posted at the same time :- )))

                K Offline
                K Offline
                kenchan
                wrote on last edited by
                #11

                @Dariusz
                Sure , no problem. I just made a few edits to my last post to clean it up a bit.
                Good luck with your OpenGL stuff.

                D 1 Reply Last reply
                0
                • K kenchan

                  @Dariusz
                  Sure , no problem. I just made a few edits to my last post to clean it up a bit.
                  Good luck with your OpenGL stuff.

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

                  @kenchan
                  Thanks, saw it, sadly still no luck, the app still has issues even when I use the painter save/restore with begin paintings. The stylesheet break it all and I have no idea how to remove it from the widget but still have control over background & text fonts in graphicsview/scene :/ I guess for scene I could just draw a QRect the size of viewport but to control font color... not sure... hmm joy :- )

                  Will post if I have any luck with it.

                  K 1 Reply Last reply
                  0
                  • D Dariusz

                    @kenchan said in Accelerate QGraphicsView with openGL widget - correct up to date workflow?:

                    Interesting thanks! I just added a one file example above your post. Looks like we posted at the same time :- )))

                    K Offline
                    K Offline
                    kenchan
                    wrote on last edited by
                    #13

                    @Dariusz
                    I have never done OpenGL directly in the scene drawBackground function, only the QGraphicsView. I will try your code out and get back to you if I have any useful suggestions.

                    1 Reply Last reply
                    0
                    • D Dariusz

                      @kenchan
                      Thanks, saw it, sadly still no luck, the app still has issues even when I use the painter save/restore with begin paintings. The stylesheet break it all and I have no idea how to remove it from the widget but still have control over background & text fonts in graphicsview/scene :/ I guess for scene I could just draw a QRect the size of viewport but to control font color... not sure... hmm joy :- )

                      Will post if I have any luck with it.

                      K Offline
                      K Offline
                      kenchan
                      wrote on last edited by kenchan
                      #14

                      @Dariusz
                      So what are you styling in your stylesheet?
                      EDIT. Ah i see it in the main function, is that all you are doing with styling?

                      D 1 Reply Last reply
                      0
                      • K kenchan

                        @Dariusz
                        So what are you styling in your stylesheet?
                        EDIT. Ah i see it in the main function, is that all you are doing with styling?

                        D Offline
                        D Offline
                        Dariusz
                        wrote on last edited by
                        #15

                        @kenchan I use mostly use this https://github.com/ColinDuquesnoy/QDarkStyleSheet and then I adjust it & push it further to get it to look nice. But for this example I posted above I just made a simple test to show the error.

                        Right now I'm trying to figure out how I can paint a rect the size of my visible viewport but that is producting some... "interesting" results. Have a look -> replace this function to see it has some interesting issues :- )

                        void gView::drawBackground(QPainter *painter, const QRectF &rect) {
                            if (painter->paintEngine()->type() == QPaintEngine::OpenGL2) {
                                painter->save();
                                painter->beginNativePainting();
                                //...
                                //do your super OPenGL stuff here
                                //        ...
                        
                                const int gridSize = 20;
                        
                                qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
                                qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
                        
                                QVarLengthArray<QLineF, 100> lines;
                        
                                for (qreal x = left; x < rect.right(); x += gridSize)
                                    lines.append(QLineF(x, rect.top(), x, rect.bottom()));
                                for (qreal y = top; y < rect.bottom(); y += gridSize)
                                    lines.append(QLineF(rect.left(), y, rect.right(), y));
                        
                                //QPen penHLines(QColor(75, 75, 75), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin);
                                //painter->setPen(penHLines);
                                //painter->drawLines(lines.data(), lines.size());
                        
                        
                                QRectF rec(viewport()->rect());
                                rec.setTopLeft({0 - rec.width() / 2, 0 - rec.height() / 2});
                                QBrush b(QColor(75, 15, 34, 255));
                        
                                painter->setBrush(b);
                                painter->setPen(QPen());
                                painter->drawRect(rec);
                        
                        
                                painter->endNativePainting();
                                painter->restore();
                            } else {
                                QGraphicsView::drawBackground(painter, rect);
                            }
                        }```
                        D 1 Reply Last reply
                        0
                        • D Dariusz

                          @kenchan I use mostly use this https://github.com/ColinDuquesnoy/QDarkStyleSheet and then I adjust it & push it further to get it to look nice. But for this example I posted above I just made a simple test to show the error.

                          Right now I'm trying to figure out how I can paint a rect the size of my visible viewport but that is producting some... "interesting" results. Have a look -> replace this function to see it has some interesting issues :- )

                          void gView::drawBackground(QPainter *painter, const QRectF &rect) {
                              if (painter->paintEngine()->type() == QPaintEngine::OpenGL2) {
                                  painter->save();
                                  painter->beginNativePainting();
                                  //...
                                  //do your super OPenGL stuff here
                                  //        ...
                          
                                  const int gridSize = 20;
                          
                                  qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
                                  qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
                          
                                  QVarLengthArray<QLineF, 100> lines;
                          
                                  for (qreal x = left; x < rect.right(); x += gridSize)
                                      lines.append(QLineF(x, rect.top(), x, rect.bottom()));
                                  for (qreal y = top; y < rect.bottom(); y += gridSize)
                                      lines.append(QLineF(rect.left(), y, rect.right(), y));
                          
                                  //QPen penHLines(QColor(75, 75, 75), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin);
                                  //painter->setPen(penHLines);
                                  //painter->drawLines(lines.data(), lines.size());
                          
                          
                                  QRectF rec(viewport()->rect());
                                  rec.setTopLeft({0 - rec.width() / 2, 0 - rec.height() / 2});
                                  QBrush b(QColor(75, 15, 34, 255));
                          
                                  painter->setBrush(b);
                                  painter->setPen(QPen());
                                  painter->drawRect(rec);
                          
                          
                                  painter->endNativePainting();
                                  painter->restore();
                              } else {
                                  QGraphicsView::drawBackground(painter, rect);
                              }
                          }```
                          D Offline
                          D Offline
                          Dariusz
                          wrote on last edited by Dariusz
                          #16

                          @Dariusz

                          Ok this appear to be somewhat working... I wonder how pricey it is comparing to a proper "workflow" of achieving this ?

                          void gView::drawBackground(QPainter *painter, const QRectF &rect) {
                              if (painter->paintEngine()->type() == QPaintEngine::OpenGL2) {
                                  
                                  painter->save();
                                  painter->beginNativePainting();
                          
                                  
                                  const int gridSize = 20;
                          
                                  qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
                                  qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
                          
                                  QVarLengthArray<QLineF, 100> lines;
                          
                                  for (qreal x = left; x < rect.right(); x += gridSize)
                                      lines.append(QLineF(x, rect.top(), x, rect.bottom()));
                                  for (qreal y = top; y < rect.bottom(); y += gridSize)
                                      lines.append(QLineF(rect.left(), y, rect.right(), y));
                                  
                          
                                  QRectF rec(-2000,-2000,4000,4000);
                                  QBrush b(QColor(75, 15, 34, 255));
                                  QPen myPen(Qt::NoPen);
                                  painter->setBrush(b);
                                  painter->setPen(myPen);
                                  painter->drawRect(rec);
                          
                                  QPen penHLines(QColor(75, 75, 75), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin);
                                  painter->setPen(penHLines);
                                  painter->drawLines(lines.data(), lines.size());
                          
                                  painter->endNativePainting();
                                  painter->restore();
                              } else {
                                  QGraphicsView::drawBackground(painter, rect);
                              }
                          }
                          

                          EDIT. Ok so Instead of doing in in view, I decided to do it in scene, this way every view will pick the same "look", as well as I can just use rect and just fill it...

                          void gScene::drawBackground(QPainter *painter, const QRectF &rect) {
                              if (painter->paintEngine()->type() == QPaintEngine::OpenGL2) {
                          
                                  painter->save();
                                  painter->beginNativePainting();
                          
                          
                                  const int gridSize = 20;
                          
                                  qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
                                  qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
                          
                                  QVarLengthArray<QLineF, 100> lines;
                          
                                  for (qreal x = left; x < rect.right(); x += gridSize)
                                      lines.append(QLineF(x, rect.top(), x, rect.bottom()));
                                  for (qreal y = top; y < rect.bottom(); y += gridSize)
                                      lines.append(QLineF(rect.left(), y, rect.right(), y));
                          
                          
                                  QBrush b(QColor(75, 15, 34, 255));
                                  QPen myPen(Qt::NoPen);
                                  painter->setBrush(b);
                                  painter->setPen(myPen);
                                  painter->drawRect(rect);
                          
                                  QPen penHLines(QColor(75, 75, 75), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin);
                                  painter->setPen(penHLines);
                                  painter->drawLines(lines.data(), lines.size());
                          
                                  painter->endNativePainting();
                                  painter->restore();
                              } else {
                                  QGraphicsScene::drawBackground(painter, rect);
                              }
                          }
                          

                          This also mean that I don't have to subclass qOpenGLWidget, and I can just use it natively + setFormat on it to get AA to work.

                          K 1 Reply Last reply
                          0
                          • D Dariusz

                            @Dariusz

                            Ok this appear to be somewhat working... I wonder how pricey it is comparing to a proper "workflow" of achieving this ?

                            void gView::drawBackground(QPainter *painter, const QRectF &rect) {
                                if (painter->paintEngine()->type() == QPaintEngine::OpenGL2) {
                                    
                                    painter->save();
                                    painter->beginNativePainting();
                            
                                    
                                    const int gridSize = 20;
                            
                                    qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
                                    qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
                            
                                    QVarLengthArray<QLineF, 100> lines;
                            
                                    for (qreal x = left; x < rect.right(); x += gridSize)
                                        lines.append(QLineF(x, rect.top(), x, rect.bottom()));
                                    for (qreal y = top; y < rect.bottom(); y += gridSize)
                                        lines.append(QLineF(rect.left(), y, rect.right(), y));
                                    
                            
                                    QRectF rec(-2000,-2000,4000,4000);
                                    QBrush b(QColor(75, 15, 34, 255));
                                    QPen myPen(Qt::NoPen);
                                    painter->setBrush(b);
                                    painter->setPen(myPen);
                                    painter->drawRect(rec);
                            
                                    QPen penHLines(QColor(75, 75, 75), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin);
                                    painter->setPen(penHLines);
                                    painter->drawLines(lines.data(), lines.size());
                            
                                    painter->endNativePainting();
                                    painter->restore();
                                } else {
                                    QGraphicsView::drawBackground(painter, rect);
                                }
                            }
                            

                            EDIT. Ok so Instead of doing in in view, I decided to do it in scene, this way every view will pick the same "look", as well as I can just use rect and just fill it...

                            void gScene::drawBackground(QPainter *painter, const QRectF &rect) {
                                if (painter->paintEngine()->type() == QPaintEngine::OpenGL2) {
                            
                                    painter->save();
                                    painter->beginNativePainting();
                            
                            
                                    const int gridSize = 20;
                            
                                    qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
                                    qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
                            
                                    QVarLengthArray<QLineF, 100> lines;
                            
                                    for (qreal x = left; x < rect.right(); x += gridSize)
                                        lines.append(QLineF(x, rect.top(), x, rect.bottom()));
                                    for (qreal y = top; y < rect.bottom(); y += gridSize)
                                        lines.append(QLineF(rect.left(), y, rect.right(), y));
                            
                            
                                    QBrush b(QColor(75, 15, 34, 255));
                                    QPen myPen(Qt::NoPen);
                                    painter->setBrush(b);
                                    painter->setPen(myPen);
                                    painter->drawRect(rect);
                            
                                    QPen penHLines(QColor(75, 75, 75), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin);
                                    painter->setPen(penHLines);
                                    painter->drawLines(lines.data(), lines.size());
                            
                                    painter->endNativePainting();
                                    painter->restore();
                                } else {
                                    QGraphicsScene::drawBackground(painter, rect);
                                }
                            }
                            

                            This also mean that I don't have to subclass qOpenGLWidget, and I can just use it natively + setFormat on it to get AA to work.

                            K Offline
                            K Offline
                            kenchan
                            wrote on last edited by kenchan
                            #17

                            @Dariusz

                            Yes i have it doing the same thing now.
                            The thing is, the startNativePainting() endNativePainting() things are supposed to be where you put raw OpenGL calls and such. You don't need them if you use the painter calls and you probably don't need to save the painter state unless it messes up what you want to do.
                            Just using a QOpenGLWidget as the viewport widget enables OpenGL acceleration for the painter, or at least that is what the Doc says here.

                            D 1 Reply Last reply
                            0
                            • K kenchan

                              @Dariusz

                              Yes i have it doing the same thing now.
                              The thing is, the startNativePainting() endNativePainting() things are supposed to be where you put raw OpenGL calls and such. You don't need them if you use the painter calls and you probably don't need to save the painter state unless it messes up what you want to do.
                              Just using a QOpenGLWidget as the viewport widget enables OpenGL acceleration for the painter, or at least that is what the Doc says here.

                              D Offline
                              D Offline
                              Dariusz
                              wrote on last edited by
                              #18

                              @kenchan
                              Interesting thanks!

                              Well I got it solved with code above, so I'll mark the topic as solved. Thanks so much for your time & help!

                              K 1 Reply Last reply
                              0
                              • D Dariusz

                                @kenchan
                                Interesting thanks!

                                Well I got it solved with code above, so I'll mark the topic as solved. Thanks so much for your time & help!

                                K Offline
                                K Offline
                                kenchan
                                wrote on last edited by
                                #19

                                @Dariusz
                                Great, glad to hear it was of some use to you.

                                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