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?

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

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 2 Posters 9.0k 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.
  • 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