Results 1 to 3 of 3

Thread: I cannot translate a QPainter on the background of a scene

  1. #1
    Join Date
    Feb 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default I cannot translate a QPainter on the background of a scene

    Hi all,

    I have an SVG (a square with some contents) that I need to be fully visible on a QGraphicsView, and its origin I need it on the center of the SVG. What I am doing right now, (in PyQt5) in the __init__ method of the QGraphicsScene subclass, is:

    # open the image
    self.renderer = QtSvg.QSvgRenderer(target)

    # the QGraphicsView widget has this size
    self.imageRect = QtCore.QSize(550,550)

    # instantiate a QPainter, with its image
    self.image = QtGui.QImage(self.imageRect, QtGui.QImage.Format_ARGB32)
    self.painter = QtGui.QPainter(self.image)

    # render the SVG onto the image
    self.renderer.render(self.painter)

    # set the image as the background brush of the scene
    self.setBackgroundBrush(QtGui.QBrush(self.image))

    # translate the painter
    self.painter.translate(-550/2, -550/2)

    Wait I get is an image whose center is (0,0), but this (0,0) is still the top-left corner. If I set the sceneRect of the QGraphicsView to be

    targetView.setSceneRect(-550/2, -550/2, 550, 550)

    Then I get the bottom-right corner of the original image only, but this time properly centered in the QGraphicsView.

    Any idea on how can I get this image centered in the view, with the origin on the center of it?

    Thank you!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: I cannot translate a QPainter on the background of a scene

    The default behaviour is to tile the background brush outward from the origin with the lower-left corner of the brush at the origin. However, a brush can carry a transform that is composed with the painter transform,and this can be used to offset the brush. The example below demonstrates the default behaviour and using the brush offset to alter it (see the comment).

    If you want to suppress the tiling then I suspect you need to customise the drawBackground() method of the scene.
    The background could also be installed on the view rather than the scene.

    Qt Code:
    1. import sys
    2. from PyQt5.QtGui import QImage, QPainter, QColor, QBrush, QLinearGradient, QTransform
    3. from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView
    4.  
    5. if __name__ == '__main__':
    6. app = QApplication(sys.argv)
    7.  
    8. # A background image
    9. bgImage = QImage(250, 250, QImage.Format_ARGB32)
    10. painter = QPainter(bgImage)
    11. gradient = QLinearGradient(0, 0, 250, 0)
    12. gradient.setColorAt(0, QColor( 0, 0, 255))
    13. gradient.setColorAt(1, QColor(255, 255, 255))
    14. painter.setBrush(gradient)
    15. painter.setPen(QColor(255, 0, 0))
    16. painter.drawRect(0, 0, 250, 250)
    17.  
    18. brush = QBrush(bgImage)
    19. transform = QTransform()
    20. transform.translate(-125, -125)
    21. # Try it without, and then with, the following line
    22. #brush.setTransform(transform)
    23.  
    24. s.setBackgroundBrush(brush)
    25. # small reference circle around the origin
    26. c = s.addEllipse(-20, -20, 40, 40)
    27.  
    28. w.setSceneRect(-550, -550, 1100, 1100);
    29. w.show()
    30.  
    31. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    d_stranz (2nd March 2019)

  4. #3
    Join Date
    Feb 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: I cannot translate a QPainter on the background of a scene

    Thank you very much!!! :-)

Similar Threads

  1. Replies: 1
    Last Post: 14th March 2017, 05:32
  2. How to set QPainter Background color?
    By marc2050 in forum Newbie
    Replies: 2
    Last Post: 29th April 2011, 05:29
  3. Drawing background of a scene
    By maverick_pol in forum Qt Programming
    Replies: 0
    Last Post: 11th January 2008, 11:12
  4. background painting in the Scene
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 7th January 2008, 19:56
  5. Scrolling problem with background using View/Scene
    By nileshsince1980 in forum Qt Programming
    Replies: 5
    Last Post: 22nd October 2007, 08:19

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.