Results 1 to 5 of 5

Thread: QWidget::render() paint to painter with wrong offset.

  1. #1
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QWidget::render() paint to painter with wrong offset.

    Hi,

    I made a custom delegate to my view (list view in a icon mode).

    This is a paint method of the delegate:
    Qt Code:
    1. void PreviewWidget::paint(QPainter *painter, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const
    3. {
    4. painter->save();
    5.  
    6. QBrush brush;
    7. int r = ((double)qrand()/ static_cast<double>( RAND_MAX )) * 255;
    8. int g= ((double)qrand()/ static_cast<double>( RAND_MAX )) * 255;
    9. int b = ((double)qrand()/ static_cast<double>( RAND_MAX )) * 255;
    10. brush.setColor(QColor(r,g,b));
    11. brush.setStyle(Qt::SolidPattern);
    12. painter->fillRect(option.rect, brush);
    13.  
    14. ImagePrev i;
    15. i.render(painter, option.rect.topLeft());
    16.  
    17. painter->restore();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Results of rendering widget onto the painter is wrong. Filling painter with random color rect is only to show where widget should be located.

    qt-problem.png

    Why this offset occur?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QWidget::render() paint to painter with wrong offset.

    Why this offset occur?
    Probably because you are passing option.rect.topLeft() into the render() call instead of (0,0).

  3. #3
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget::render() paint to painter with wrong offset.

    But option.rect.topLeft() is a good position for every item. First will get (0,0), next will get another position according to size of a item (given by sizeHint()). So unfortunately your comment is very wrong.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QWidget::render() paint to painter with wrong offset.

    Here's a bug report on this: QTBUG-26694

    Try the workaround in the attachment:
    Qt Code:
    1. i.render( painter, painter->deviceTransform().map(option.rect.topLeft()));
    To copy to clipboard, switch view to plain text mode 

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

    m.p (25th February 2013)

  6. #5
    Join Date
    May 2011
    Posts
    19
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget::render() paint to painter with wrong offset.

    Quote Originally Posted by norobro View Post
    Here's a bug report on this: QTBUG-26694

    Try the workaround in the attachment:
    Qt Code:
    1. i.render( painter, painter->deviceTransform().map(option.rect.topLeft()));
    To copy to clipboard, switch view to plain text mode 
    It works. Thanks a lot!

Similar Threads

  1. WebKit layout translates painter, how to get offset?
    By omnilala in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 24th January 2012, 23:13
  2. Replies: 1
    Last Post: 2nd November 2011, 20:30
  3. Replies: 8
    Last Post: 9th November 2010, 21:11
  4. Replies: 5
    Last Post: 20th October 2009, 07:18
  5. Painter greyscale, image quality paint-brush!
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 22nd March 2007, 20:32

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.