March 31, 2011

billouparis billouparis
Ant Farmer
142 posts

QGraphicsPixmapItem QGraphicsRotation NO ANTIALIASING!

 

Hello,

I am trying to display a simple pixmap item (based on a PNG file) iniside my scene and apply a 3D rotation to it using QGraphicsRotation (rotation around the Y axis). And I would like to get the pixmap drawn with antialiasing.

I have to set the RenderHints to (QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing); in my QGraphicsView constructor, but the drawing of the QGraphicsPixmapItem was still without antialiasing.

So I derived my own QGraphicsPixmapItem class, to be able to overload the paint method. I added a

  1.  painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
in the paint method, and then call QGraphicsPixmapItem::paint(painter, option, widget), but I still did not get any antialiasing.

I have the same isssue either on Windows XP, or under Linux X11.

In the paint method of my derived class, if I call paint->drawPixmap(…) then the pixmap is drawn correctly, I mean with antialiasing.

Can somebody explain to me why is there no antialiasing in my previous described cases? Thank you!

Edit: adapted text a bit to fix rendering; Andre

8 replies

March 31, 2011

ZapB ZapB
Robot Herder
1355 posts

Well, what does QGraphicsPixmapItem::paint() do that is different to what you do? Does it for example override your renderhints? Is it set to use device coordinate caching?

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 1, 2011

billouparis billouparis
Ant Farmer
142 posts

“Well, what does QGraphicsPixmapItem::paint() do that is different to what you do?”

-> I don’t know, but apprently it does not change my renderhints apparently.

“Is it set to use device coordinate caching?”

-> I don’t know, I just tried to set the caching mode myselft and tried the three options, none of them changed anything.

April 1, 2011

ZapB ZapB
Robot Herder
1355 posts

Don’t be afraid to look into the Qt source – it won’t bite you ;-). A 2 minute search of the Qt source code reveals this:

  1. void QGraphicsPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
  2.                                 QWidget *widget)
  3. {
  4.     Q_UNUSED(widget);
  5.  
  6.     painter->setRenderHint(QPainter::SmoothPixmapTransform,
  7.                            (d->transformationMode == Qt::SmoothTransformation));
  8.  
  9.     painter->drawPixmap(d->offset, d->pixmap);
  10.  
  11.     if (option->state & QStyle::State_Selected)
  12.         qt_graphicsItem_highlightSelected(this, painter, option);
  13. }

So the paint function does set a render hint. Notably the QPainter::SmoothPixmapTransform renderhint. So it sets this to true iff

  1. d->transformationMode == Qt::SmoothTransformation

So how do we get this to evaluate to true? A quick look at the docs shows me this function [doc.qt.nokia.com].

I suggest you call this function and set the transformation mode to true once you have created your item. Then you should get anti-aliased drawing (unless you override it with something else in your derived paint event of course) ;-)

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 1, 2011

billouparis billouparis
Ant Farmer
142 posts

The problem I actually have is debugging under Linux, when I press F11 on the call to the paint line, IT SIMPLY WON’T DISPLAY ANY SOURCE CODE AT ALL. This GDB thing is driving me crazy, I tried days and days to get the source code to get displayed but it simply wouldn’t.

Anyway I tried on PC, and here I can access the source code alas.
So I set the Qt::SmoothTransformation mode as you mentionned and IT IS ACTUALLY WORKING Now!

I thank you for your answer to my question. We can now close this thread.
Thank you ZapB.

Regards,
Bill

April 1, 2011

ZapB ZapB
Robot Herder
1355 posts

Well to step into the Qt code in the debugger you need a debug build of Qt. You can open the source in your ide though.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 1, 2011

billouparis billouparis
Ant Farmer
142 posts

I think my Qt is compiled with debug option, though I have to triple check this.

Now, i am still stuck with pixmap items being grouped in a pixmap group, the rendering is still not antialiased for those.
Have to check this now.

Thank you ZapB

April 1, 2011

ZapB ZapB
Robot Herder
1355 posts

You’re welcome. Feel free to come back with more questions if you can’t resolve it. Code snippets (or even better compilable examples) are always useful.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 1, 2011

billouparis billouparis
Ant Farmer
142 posts

I couldn’t find a simple solution to apply a QGraphicsRotation directly to a QGraphicsItemGroup and have my items been displayed with antialiasing, so I simply apply the QGraphicsRotation to each items included in my group to have something decent.
Thank you,

Bill

 
  ‹‹ Qt 4.7.1 and QImage::convertToFormat      [edit] ActiveQt dynamicCall error: bad parameter count despite the right number ››

You must log in to post a reply. Not a member yet? Register here!