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. How to setup a QPainter/QImage to save PNG of a GraphicsView?
Forum Updated to NodeBB v4.3 + New Features

How to setup a QPainter/QImage to save PNG of a GraphicsView?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 6.8k 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.
  • R Offline
    R Offline
    rivimey
    wrote on last edited by
    #1

    Folks,
    I have a custom widget that is based on QGraphicsView and which displays a number of Items. I want to be able to render the current View to an QImage which I then save as a PNG file. I want the file to be rendered at higher resolution than the screen: in the code here I've picked arbitrary numbers, but ideally I'd find out the aspect ratio and select a size based on a scene size -> dpi mapping of some sort.

    I have the current code

    @ img = QtGui.QImage(2048, 2048, QtGui.QImage.Format_RGB32)
    p = QtGui.QPainter(img)
    p.setViewTransformEnabled(True)
    p.setWindow(self.map.scene.sceneRect().toRect())

        img.fill(QtCore.Qt.white)
        p.scale(1,-1)
        
        # Render the current view...
        self.map.scene.render(p)
        p.end()
        
        # and save it.
        img.save(fileName)
    

    @

    Which I was hoping would render the view ok, but it is rendered off-centre, with about 1/2 clipped on the top.

    Any ideas how I can correct this, and is there anything else to consider too about using the widget in this way?

    Thanks,Ruth

    1 Reply Last reply
    0
    • S Offline
      S Offline
      steno
      wrote on last edited by
      #2

      I would try not to set the window or the view transform enabled. If you call render from the scene, it will use the rectangle of the paint device for the target and the sceneRect for the source. That's how I implemented the same thing.

      @
      QImage pixmap(size, QImage::Format_ARGB32);
      pixmap.fill(Qt::white); //fill the pixmap with white.

      QPainter imagePainter(&pixmap);
      imagePainter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);

      mScene->render(&imagePainter);
      @

      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