Results 1 to 2 of 2

Thread: QLabel and its content (pixmap) position problem.

  1. #1
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QLabel and its content (pixmap) position problem.

    Hi Im using QLabel to display an image like bellow:
    Qt Code:
    1. mLabel = new QLabel();
    2. mLabel->setBackgroundRole(QPalette::Base);
    3. mLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    4. mLabel->setPixmap(QPixmap::fromImage(mImage));
    To copy to clipboard, switch view to plain text mode 

    I would like to know the correct position of the pixmap's first top-left pixel that is draw, relevant to QLabel. How I can get this info ? I'm writing an image color picker based on mouse position and I need to know the top and left offset of the QLabel's pixmap. I've tried several methods :
    mLabel->frameGeometry().x();
    mLabel->frameGeometry().y();
    mLabel->margin();
    mLabel->indent();
    mLabel->contentsMargins().top();
    mLabel->contentsMargins().left();
    mLabel->contentsRect();
    mLabel->getContentsMargins(&left, &top, &right, &bottom);
    but none of them give me the correct result

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel and its content (pixmap) position problem.

    Hi there!

    Have a look at qlabel.cpp and voidQLabel::paintEvent(QPaintEvent*)

    Qt Code:
    1. QRect cr = contentsRect();
    2. cr.adjust(d->margin, d->margin, -d->margin, -d->margin);
    3. ..
    4. if (d->pixmap && !d->pixmap->isNull()) {
    5. QPixmap pix;
    6. if (d->scaledcontents) {
    7. if (!d->scaledpixmap || d->scaledpixmap->size() != cr.size()) {
    8. if (!d->cachedimage)
    9. d->cachedimage = new QImage(d->pixmap->toImage());
    10. delete d->scaledpixmap;
    11. d->scaledpixmap = new QPixmap(QPixmap::fromImage(d->cachedimage->scaled(cr.size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    12. }
    13. pix = *d->scaledpixmap;
    14. } else
    15. pix = *d->pixmap;
    16. opt.initFrom(this);
    17. if (!isEnabled())
    18. pix = style->generatedIconPixmap(QIcon::Disabled, pix, &opt);
    19. style->drawItemPixmap(&painter, cr, align, pix);
    20. }
    To copy to clipboard, switch view to plain text mode 


    You will have to take care of those options: scaledcontents and alignment.

    If one could get the value somehow different - the Qt code would probably use it..

    The style draws the pixmap like this:

    Qt Code:
    1. void QStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment,
    2. const QPixmap &pixmap) const
    3. {
    4. QRect aligned = alignedRect(QApplication::layoutDirection(), QFlag(alignment), pixmap.size(), rect);
    5. QRect inter = aligned.intersected(rect);
    6.  
    7. painter->drawPixmap(inter.x(), inter.y(), pixmap, inter.x() - aligned.x(), inter.y() - aligned.y(), inter.width(), inter.height());
    8. }
    To copy to clipboard, switch view to plain text mode 


    Edit:
    Be careful, the used drawPixmap function, doesn't necessarily draw the topleft of the pixmap to x,y.

    From the docs:

    void QPainter::drawPixmap ( int x, int y, const QPixmap & pixmap, int sx, int sy, int sw, int sh )
    This is an overloaded function.

    Draws a pixmap at (x, y) by copying a part of the given pixmap into the paint device.

    (x, y) specifies the top-left point in the paint device that is to be drawn onto. (sx, sy) specifies the top-left point in pixmap that is to be drawn. The default is (0, 0).

    (sw, sh) specifies the size of the pixmap that is to be drawn. The default, (0, 0) (and negative) means all the way to the bottom-right of the pixmap.

    Johannes
    Last edited by JohannesMunk; 21st September 2010 at 11:13.

Similar Threads

  1. Pixmap updating QLabel
    By Matt in forum Newbie
    Replies: 11
    Last Post: 17th August 2010, 22:11
  2. How to change the content's position in viewport?
    By aaron in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2009, 09:37
  3. Saving QLabel content in a file
    By Caius Aérobus in forum Newbie
    Replies: 1
    Last Post: 29th October 2008, 18:03
  4. Inaccurate MouseEvent position over a Scaled Pixmap
    By ramstormrage in forum Newbie
    Replies: 9
    Last Post: 26th May 2008, 11:48
  5. Save content of widget to pixmap
    By vql in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2008, 23:26

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.