Results 1 to 11 of 11

Thread: Efficient Scaling and Rotating of QGraphicsView

  1. #1
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Efficient Scaling and Rotating of QGraphicsView

    I currently have a function which I call to scale and rotate a QGraphicsView.

    The function is:
    Qt Code:
    1. void theForm::scaleView(qreal scaleFactor)
    2. {
    3. qreal scale = pow(2.0, (scaleFactor));
    4. QMatrix matrix;
    5. matrix.scale(scale, scale);
    6. matrix.rotate(levelRotate); // LevelRotate is a global variable
    7. ui.graphicsView->setMatrix(matrix);
    8. }
    To copy to clipboard, switch view to plain text mode 

    The QGraphicsView has a Pixmap drawn on it from a drawBackground function override.

    The function is:
    Qt Code:
    1. void Override::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. ...
    4. painter->drawPixmap(0,0, theForm->getBackground()); // getBackground() returns a pixmap
    5. }
    To copy to clipboard, switch view to plain text mode 

    This above code worked fine at scaling and rotating when getBackground() was returning a reasonable sized (~2mb | 512x600) QPixmap. But I increased the image size (~10mb | 2400x4600) that I was testing with. Now that I've implemented a much larger image, it lags far to much to be useful. So I had a couple questions. Does anyone know of a more efficient way to scale and rotate an image? I tried to call the QGraphicsView's scale and rotate functions directly (i.e graphicsView->rotate(1.2)), without using a QMatrix. That had no effect in speed, and I'm assuming those functions using a QMatrix anyways.

    My second related question is that I set the cacheMode on the QGraphicsView to be QGraphicsView::CacheBackground. Since I had overridden the the QGraphicsView with the Override class, does it still cache the background? Does the CacheBackground flag even set the program to cache items that I'm painting through the drawBackground function?

    Thanks in advance! I apologize this is so long...

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    You may try to use a GL widget as a viewport for the view. It might speed up the operation.

  3. #3
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    I've been trying to put together some code based upon what was suggested and from looking at Qt's HelloGL example. I can't seem to piece anything together and it's probably because I don't understand completely what the solution is. From the suggestion am I trying to set the QGraphicsView's viewport to be a QGLWidget (is that possible)? If so, is the purpose of doing that to replace my current drawBackground function override and do the painting within the QGLWidget? Or is the purpose of creating a QGLWidget only supposed to be used for zooming and scaling? I'm obviously a bit confused...can you give me a little more detail on your suggestion.

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    Take a look at the "chips" demo that comes with Qt.

  5. #5
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    I didn't see any increase in performance from doing:

    Qt Code:
    1. ui.graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    To copy to clipboard, switch view to plain text mode 

    I also attempted to use a variety of other format flags (i.e. NoAlphaChannel and NoOverlay) to see if that would speed up the process, but no luck.

    I checked to make sure my system has OpenGL support using QGL::hasOpenGL() and it returned true, so that doesn't seem to be an issue.

    I'm using one of NVIDIA's newer video cards, so can I assume that I wouldn't see far better results from another card.

    Does anyone have any other advice to lead me in the right direction?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    But do you use OpenGL functions for drawing the background? Using QPainter is not enough here, I think.

    Maybe your bottleneck is not drawing, rotating or scaling the pixmap but actually creating it? What does getBackground() do?

  7. #7
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    No, I don't use OpenGL to draw the background. I do use painter and here is the code:

    Qt Code:
    1. void Override::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. painter->save();
    4. painter->setBrush(Qt::black);
    5. painter->drawRect(rect);
    6. painter->drawPixmap(0,0, theForm->getBackground()); // getBackground() returns a pixmap
    7. painter->restore();
    8. }
    9.  
    10. QPixmap theForm::getBackground()
    11. {
    12. return backgroundImage; // This is an image stored in a classes QPixmap variable.
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by forrestfsu; 8th December 2006 at 18:39.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    I'm not sure, but it is possible that you need to draw the background using OpenGL code. Unfortunately "chips" doesn't draw a background, so we can't check in the example...

    The pixmap you're using is very big, so obviously transforming it is very expensive. I understand that the background is getting rotated and scaled when items get scaled and rotated, correct?

  9. #9
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    I'm not quite sure I understand what you mean by "the background is getting rotated and scaled when items get scaled and rotated." So I'll try and explain:

    This specific graphicsView has a scene which it shares with 5 other graphicsView's. The only difference with this graphicsView is that I use drawBackground() to give it a unique background Pixmap (in this case a very large pixmap). I have zoom and rotate buttons on the widget which call the ScaleView() function for this graphicsView. ScaleView() scales and rotates all the contents within this graphicsView (the scene contents and the large background Pixmap).

    Hope this clears it up.

  10. #10
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    An additional note. Once the user has scaled the image to a different ratio then the original, even using the scroll bars for the graphicsView lag moving the content of the graphicsView.

  11. #11
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Efficient Scaling and Rotating of QGraphicsView

    I just fixed the problem (still a few minor bugs to go through). Instead of returning an entire Pixmap, I needed only return the area of the Pixmap that I needed to display using the QPixmap's copy function. After I have all the code in place I will paste it for review.

    Thanks Wysota for all your help on this issue.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.