Results 1 to 3 of 3

Thread: How to apply QPainter::SmoothPixmapTransform to QGracphicsView?

  1. #1
    Join Date
    Nov 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to apply QPainter::SmoothPixmapTransform to QGracphicsView?

    Hi,

    I'm using PyQt4 (4.8.4, based on Qt 4.7.2). I want to display pixmaps on the screen every time the user presses a key. So far so good.
    But I also want to scale and rotate the images. Then bad things happen. I use the GraphicsScene framework and the setRotation item method to rotate the pixmap. This much works except that I don't seem to be able get any smoothing to work.

    Here's my code:

    Qt Code:
    1. class MainWindow(QWidget):
    2. def __init__(self):
    3. QMainWindow.__init__(self, None) #, Qt.FramelessWindowHint)
    4.  
    5. self.setWindowState(Qt.WindowFullScreen)
    6.  
    7. self.vL = QVBoxLayout()
    8. self.setLayout(self.vL)
    9.  
    10. self.scene = QGraphicsScene()
    11. self.view = QGraphicsView(self)
    12.  
    13. #~ self.view.setRenderHint(QPainter.SmoothPixmapTransform, False)
    14. self.view.setScene(self.scene)
    15.  
    16. self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    17. self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    18. self.vL.addWidget(self.view)
    19.  
    20. self.view.setRenderHints(QPainter.SmoothPixmapTransform | QPainter.Antialiasing)
    21. self.view.setSceneRect(0, 0, self.width(), self.height())
    22.  
    23. self.img_list = glob('img/*.jpg')
    24. self.img_list.extend(glob('img/*.png'))
    25.  
    26.  
    27. def keyPressEvent(self, event):
    28. event.accept()
    29. self.disp_image(self.img_list[random.choice(range(len(self.img_list)))])
    30.  
    31. def disp_image(self, file, position=None):
    32.  
    33. pxmap = QPixmap(file)
    34. i = self.scene.addPixmap(pxmap)
    35. i.setTransformOriginPoint(pxmap.width()/2., pxmap.height()/2.)
    36. i.setRotation(10)
    37. i.setPos((.8*random.random()+.1)*self.width()-pxmap.width()/2., (.8*random.random()+.1)*self.height()-pxmap.height()/2.)
    38.  
    39. self.view.show()
    To copy to clipboard, switch view to plain text mode 

    Whatever I do, the QPainter.SmoothPixmapTransform seems to be ineffective (see the attachements: one is Tux rotated using Qt, the next is Tux rotated using XnView).
    I've tried many many things: using an OpenGL viewport instead of the default raster one; declaring the hints before setting the scene; setting the hints after each item is added to the scene; even tried a method propose elsewhere where the view is retrieved from the item itself; also view.show() doesn't seem to be required or doing anything.

    I've spent the last 2 days googling this but couldn't find a clue. Someone seems to have had the same problem in 2007 but got no response...

    Any clues?
    Ah, I've tried that on WinXP only by the way.

    Thanks a lot for any hint!
    -Etienne
    Attached Images Attached Images

  2. #2
    Join Date
    Nov 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to apply QPainter::SmoothPixmapTransform to QGracphicsView?

    Gosh...
    Is it an extraordinarily tricky question, or is it the dumbest question ever asked, so dumb that it doesn't require answers ?

    I wondered if that was a license issue... there is a slightly atypical copyright statement in the QImage page... but bilinear algorithms are nothing secret or protected (although a specific optimized implementation might be). Anyway, I've ruled that out since everything's fine if I transform the QImage rather than the QGraphicsPixmapItem. This is just pretty slow (QImage, transform, convert to QPixmap, insert in scene).

    I also wanted to add that, yeah, render hints are just hints, they are not necessarily satisfied by the painting device. Fair enough. But that would be good to know when it can be expected them to be.

    In the end I've given up using pixmaps, and am only using SVG files. But that would have been nice to be able to do both, so if anyone has any clue, I'm still all ears wide open.

    Cheers.

  3. #3
    Join Date
    Jun 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: How to apply QPainter::SmoothPixmapTransform to QGracphicsView?

    Hi Etienne

    Hopefully you found the solution meanwhile. If not, and for the others who come to this thread, I found a solution to this problem at least on my platform (Qt 4.6.3 for Symbian). It is quite simple (it just requires to know the classes you are using or to have good luck when googling ).

    Before you call setRotation, call setTransformationMode(Qt::SmoothTransformation) on the bitmap.

    Calling setRenderHints on the view is even not necessary for that smooth transformation to work.

    Matej

Similar Threads

  1. How to apply font to QString
    By coolkaps in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 23:27
  2. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  3. Replies: 5
    Last Post: 7th September 2009, 20:57
  4. Where to apply transform? iterm or QPainter?
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2009, 08:40
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

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.