Results 1 to 10 of 10

Thread: Inaccurate MouseEvent position over a Scaled Pixmap

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

    Default Inaccurate MouseEvent position over a Scaled Pixmap

    Hello experts,

    Ive got this label with a pixmap on it which is scaled to fit. Now, I have subclassed this label to return the color of the pixel under the mouse cursor whenever I click on it. The problem is I dont get accurate pixel colors. However, I tried an image that is not scaled and it works perfectly.

    Is there a way to get the correct position in scaled-to-fit mode? Thank you..

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

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    May we see how you do it? Of course you have to take the scaling into account. Maybe it would be easier to write your own simple "PixmapWidget" to get direct access to the scaled pixmap.
    J-P Nurmi

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

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    Hi jpn,

    Thank you for answering..
    Ive attached the folder of my sample program.. It has the codes for the custom frame, custom label and the dialog.. Sorry if I cant make it any simpler, its as simple as it gets..

    Whenever you click inside the label, the frame on the lower right corner changes color depending on the pixel selected. If you'd try clicking around, you'll see that it doesnt work like it should..

    Id like to know more about the "simple PixmapWidget" you were suggesting earlier. How is it done?

    Thank you in advance
    Attached Files Attached Files

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

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    Quote Originally Posted by ramstormrage View Post
    Whenever you click inside the label, the frame on the lower right corner changes color depending on the pixel selected. If you'd try clicking around, you'll see that it doesnt work like it should..
    Yeah, because you get the pixel from the ORIGINAL image but the label shows a different SCALED image.

    Id like to know more about the "simple PixmapWidget" you were suggesting earlier. How is it done?
    A QFrame subclass with two private members, original pixmap and scaled pixmap. In resizeEvent() you scale the original pixmap according to width() and height() and store it as scaled pixmap. In paintEvent() you draw the scaled pixmap. In mousePressEvent() you get the pixel from the scaled pixmap.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    ramstormrage (18th May 2008)

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

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    Its a Qframe subclass not a QLabel one?
    Im not really familiar with overriding the resizeEvent() and paintEvent() handler. Could you provide me with a minimal example so I could understand it more?

    Thank you..

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

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    Quote Originally Posted by ramstormrage View Post
    Its a Qframe subclass not a QLabel one?
    Yes, because you'll draw the pixmap yourself and therefore QLabel offers nothing you're interested in.

    Im not really familiar with overriding the resizeEvent() and paintEvent() handler. Could you provide me with a minimal example so I could understand it more?
    You already reimplemented some mouse event handlers for your custom label. Reimplementing paintEvent() and resizeEvent() doesn't differ syntactically. Use QPainter to draw the pixmap in paintEvent() and QPixmap::scaled() to scale the pixmap in resizeEvent().
    J-P Nurmi

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

    Default Re: Inaccurate MouseEvent position over a Scaled Pixmap

    Hello again jpn,

    Sorry.. I dont understand how would there be a resizeEvent() in a QFrame. Could you give a me pseudocodes so I can understand more? Im lost..

    Qt Code:
    1. //I call this function when the dialog is invoked or when I want to place a new Pixmap
    2. void simplePixmapWidget::loadPixmap(QImage &img)
    3. {
    4. originalPx = new QPixmap(QPixmap::fromImage(img));
    5. newPx = new QPixmap(width(), height());
    6. }
    7.  
    8. void simplePixmapWidget::resizeEvent(QResizeEvent *ev)
    9. {
    10. QSize size(width(), height());
    11. newPx->scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
    12.  
    13. //calls paintEvent afterwards
    14. }
    15.  
    16. void simplePixmapWidget::paintEvent(QPaintEvent *ev)
    17. {
    18. QPainter(this);
    19. painter.drawPixmap(where?, newPx);
    20. }
    To copy to clipboard, switch view to plain text mode 

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

    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

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

    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.

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.