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. Antialiasing for QPixmap scaled in a QGraphicsScene
Qt 6.11 is out! See what's new in the release blog

Antialiasing for QPixmap scaled in a QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
22 Posts 8 Posters 28.4k 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.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    Hi everyone,
    I am displaying two overlayed pixmaps (with addPixmap()) in a QGraphicsView.
    I followed the classic example to add panning and zooming (done by calling scale())
    The problem is that when I zoom out, the pixmaps are badly subsampled and it results in an awful pixelized rendering.

    I tried to setup the view with Antialiasing and SmoothPixmapTransform flags but it looks like the scale() always performs an ugly fast transform.
    Is there any solution to get a better rendering?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jake007
      wrote on last edited by
      #2

      Probably not.
      It should look ok, if you scale it up or down exactly time two, or four.
      Of Course, there is alway a possibility, to write your own AA function, or download and implement one from internet.


      Code is poetry

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bu7ch3r
        wrote on last edited by
        #3

        Pixmap should render ok. Perhaps you have low quality source nested in your container.

        for(int i = 200; i > 0;)
        try
        {
        //do something
        }
        catch(...)
        {
        i--;//try again
        }

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JulienMaille
          wrote on last edited by
          #4

          Ok guys, the question is simple: is it possible to obtain a smooth transformation when calling scale() on a QGraphicsScene containing QPixmaps. (Jake007 says yes, bu7ch3r says no)
          If yes how?
          If no, Jake007: you suggests to write my own AA, does it mean subclassing QGraphicsItem and reimplementing the paintEvent?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SteveXP
            wrote on last edited by
            #5

            Hi,
            i encountered a similar behavior when scaling QGraphicsPixmapItems in an QGraphicsScene. As i would expect a scene Scale to promote to the same routines as an PixmapItem scale, the following solution might help you too:

            I set SmoothTransformation per PixmapItem (default is Qt::FastTransformation) and not only on the QGraphicsView.
            itemPixmap->setScale(xx);
            itemPixmap->setTransformationMode(Qt::SmoothTransformation);

            1 Reply Last reply
            1
            • J Offline
              J Offline
              JulienMaille
              wrote on last edited by
              #6

              Hey, did you register just to answer me? Thanks a lot!
              Calling setTransformationMode(Qt::SmoothTransformation); on the QGraphicsPixmapItem worked. Now the pixmap is blurred when I zoom in.
              But I still have a strong moiré effect when I zoom out.

              I tried to set this on the QGraphicsView, but it does not help:
              @ setRenderHint(QPainter::HighQualityAntialiasing, true);
              setRenderHint(QPainter::SmoothPixmapTransform, true);@

              1 Reply Last reply
              0
              • L Offline
                L Offline
                loading...
                wrote on last edited by
                #7

                wow, i'm a newbie and i have the same question now.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  blackpainter
                  wrote on last edited by
                  #8

                  I am having exactly the same problem. Here is a short summary what my problem is and what i've tried so far:

                  I have a Qgraphicsview with qgraphicspixmapitems in it. Now if i scale the view (zoom out), i get those ugly moire effects:

                  !http://i.imgur.com/Eu0S7a2.jpg(Moire effect)!

                  what i want though is this, scaled in photoshop:

                  !http://i.imgur.com/mwRiq03.jpg(no moire)!

                  I tried subclassing qgraphicspixmapitem and set the render hints:
                  @void xGraphicsPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
                  painter->setRenderHint( QPainter::Antialiasing, true);
                  painter->setRenderHint( QPainter::HighQualityAntialiasing, true);
                  QGraphicsPixmapItem::paint(painter, option, widget);
                  }@

                  and also this:
                  @void xGraphicsPreviewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
                  painter->setRenderHint( QPainter::Antialiasing, true);
                  painter->setRenderHint( QPainter::HighQualityAntialiasing, true);
                  painter->drawPixmap(boundingRect().topLeft(), this->pixmap());
                  }@

                  But with both the renderhints do not have any effect on the images. Any help/advice?

                  Thanks and Regards

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    Jassie
                    wrote on last edited by
                    #9

                    Hey, Have you solved this problem? Could you please tell me how?Thank you !

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jassie
                      wrote on last edited by
                      #10

                      Hey, Have you solved this problem? Could you please tell me how?Thank you !

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        blackpainter
                        wrote on last edited by
                        #11

                        Hi Jassie,

                        I had to implement it completely manually.
                        Simply create small resized versions of your image when loading it or on demand and load them into your graphicsview depending on the zoom value.

                        You get the best results when your image has the actual size in which it is displayed in. (e.g. if you zoom out so your image will only be 512px wide, resize it to 512px before loading it into the interface)

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          blackpainter
                          wrote on last edited by
                          #12

                          Hi Jassie,

                          I had to implement it completely manually.
                          Simply create small resized versions of your image when loading it or on demand and load them into your graphicsview depending on the zoom value.

                          You get the best results when your image has the actual size in which it is displayed in. (e.g. if you zoom out so your image will only be 512px wide, resize it to 512px before loading it into the interface)

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            Jassie
                            wrote on last edited by
                            #13

                            Hi,

                            Do you mean I should zoom out my image first before I scale the view?

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Jassie
                              wrote on last edited by
                              #14

                              Hi,

                              Do you mean I should zoom out my image first before I scale the view?

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                blackpainter
                                wrote on last edited by
                                #15

                                As an example:
                                I load my original image with size 1024px. It is displayed in my graphicsview with the same size.
                                Now i zoom out 50%. But instead of scaling the graphicsview, simply resize the original image to 512px (with QT or openCV) and reload it into the graphicsview.

                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  blackpainter
                                  wrote on last edited by
                                  #16

                                  As an example:
                                  I load my original image with size 1024px. It is displayed in my graphicsview with the same size.
                                  Now i zoom out 50%. But instead of scaling the graphicsview, simply resize the original image to 512px (with QT or openCV) and reload it into the graphicsview.

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    Jassie
                                    wrote on last edited by
                                    #17

                                    Thank you.But If I scale the image manually everytime I scale the view in wheelevent,it costs so much time. Is there any better way to solve this problem?

                                    1 Reply Last reply
                                    0
                                    • J Offline
                                      J Offline
                                      Jassie
                                      wrote on last edited by
                                      #18

                                      Thank you.But If I scale the image manually everytime I scale the view in wheelevent,it costs so much time. Is there any better way to solve this problem?

                                      1 Reply Last reply
                                      0
                                      • E Offline
                                        E Offline
                                        euchkatzl
                                        wrote on last edited by
                                        #19

                                        When you want the best result there is no other option as drawing the image in exact the size shown on screen.(in Pixel)

                                        To archive this you could subclass QGraphicsPixmapItem and override paint.

                                        There you have to load the pixmap in the correct size (try to map your scene coordinates to pixels).

                                        To load an Image in an scaled size you could for example use QImageReader :

                                        http://doc.qt.io/qt-5/qimagereader.html#setScaledSize

                                        Of course you have to cache the loaded Pixmap yourself.
                                        When you reach paint event with the same size your code should use the cached pixmap.

                                        1 Reply Last reply
                                        0
                                        • E Offline
                                          E Offline
                                          euchkatzl
                                          wrote on last edited by
                                          #20

                                          When you want the best result there is no other option as drawing the image in exact the size shown on screen.(in Pixel)

                                          To archive this you could subclass QGraphicsPixmapItem and override paint.

                                          There you have to load the pixmap in the correct size (try to map your scene coordinates to pixels).

                                          To load an Image in an scaled size you could for example use QImageReader :

                                          http://doc.qt.io/qt-5/qimagereader.html#setScaledSize

                                          Of course you have to cache the loaded Pixmap yourself.
                                          When you reach paint event with the same size your code should use the cached pixmap.

                                          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