Results 1 to 10 of 10

Thread: Inaccurate MouseEvent position over a Scaled Pixmap

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    First of all, there is no need to allocate QPixmaps on the heap. It's an implicitly shared class:
    Qt Code:
    1. // QPixmap* originalPx;
    2. // QPixmap* newPx;
    3. QPixmap originalPx;
    4. QPixmap newPx;
    5.  
    6. originalPx = QPixmap::fromImage(img);
    7. newPx = QPixmap(width(), height());
    To copy to clipboard, switch view to plain text mode 
    Notice that QPixmap::scaled() RETURNS the scaled pixmap and you might want to always scale the original pixmap not to lose information over time:
    Qt Code:
    1. newPx = originalPx.scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
    To copy to clipboard, switch view to plain text mode 
    Because you ignore aspect ratio and the pixmap size always matches widget size, you can just draw the pixmap in the upper left corner (0,0):
    Qt Code:
    1. painter.drawPixmap(0, 0, newPx);
    To copy to clipboard, switch view to plain text mode 
    If you later decide to keep aspect ratio, then you can use this handy method in QStyle to draw the pixmap as aligned:
    Qt Code:
    1. style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, newPx);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  2. #2
    Join Date
    Dec 2007
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    Hi jpn,

    Thank you for the clarifications.
    I did some modifications and I placed the codes from the resizeEvent to loadPixmap() because I wanted to be able to change the Pixmap whenever necessary.

    Qt Code:
    1. void simplePixmapWidget::loadPixmap(QImage &img)
    2. {
    3. origPx = QPixmap::fromImage(img);
    4. newPx = QPixmap(width(), height());
    5. QSize size(width(), height());
    6. newPx = origPx.scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
    7. QImag0 = newPx.toImage();
    8. update();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Thank you very much.
    Last edited by ramstormrage; 26th May 2008 at 10:58.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    Call QWidget::update() to schedule a paint event.
    J-P Nurmi

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
  •  
Qt is a trademark of The Qt Company.