Results 1 to 7 of 7

Thread: QPixmap scaling question

  1. #1
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QPixmap scaling question

    Hello.
    I create two QPixmaps: one is loaded from a bitmap file, the other one is drawn. The first one can be rescaled with scaled(), the second one can not. Do you know why? Thank you!

    This scaling works
    Qt Code:
    1. QPixmap pm(600,450);
    2. pm.load("image.jpg");
    3. pm = pm.scaled(300,225); //this works fine
    4. pixmapLabel->setPixmap(pm);
    To copy to clipboard, switch view to plain text mode 

    This scaling won't work:
    Qt Code:
    1. QPixmap pm(600,450);
    2. QPainter p(&pm);
    3. QPen pen(Qt::red, 2);
    4. p.setPen(pen);
    5. p.drawLine(0, 0, 600, 450);
    6. pm = pm.scaled(300,225);//this has no effect. why?
    7. pixmapLabel->setPixmap(pm);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap scaling question

    Come on... You draw a diagonal line, scale it to half and expect to see a difference? The line is the same(diagonal). Try drawing something else.

  3. #3
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap scaling question

    Thanks Marcel, but whatever I draw I don't see the difference (the picture is not getting smaller when I scale()) - the overall size of the pixmap is unchanged. There must be something else wrong. I've tested this with all kinds of other shapes, too.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap scaling question

    What does pm.size() returns after scaling? It should return (300, 225). If so, then the scaling is successful.

  5. #5
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPixmap scaling question

    Hi Marcel. Thanks for helping me.

    The scaling does not take place because the 1) pixmap is displayed in original size, and
    2) the size as returned by height() and width() has not changed. I put the code below.

    Qt Code:
    1. QPixmap pm(600,450);
    2. QPainter p(&pm);
    3. QPen pen(Qt::red, 2);
    4. p.setPen(pen);
    5. p.drawLine(0, 0, 600, 450);
    6. pm = pm.scaled(300,225);
    7.  
    8. xLabel->setNum(pm.width());
    9. yLabel->setNum(pm.height());
    10. pixmapLabel->setPixmap(pm);
    To copy to clipboard, switch view to plain text mode 

    P.S. I'm drawing a straight line here in order to be able to easily tell cropping from rescaling. If cropping takes place, the line is no longer perfect diagonal. That's why I chose to draw a line in this test code.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap scaling question

    I think this might be because the painter is still opened on the pixmap when you scale it.
    Try calling p.end() before scaling the pixmap.

  7. The following user says thank you to marcel for this useful post:

    eric (20th November 2007)

  8. #7
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default problem solved

    Marcel, you ROCK!

    That solved my problem.
    Thank you.

Similar Threads

  1. QPixmap and HBITMAP
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 16:24

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.