PDA

View Full Version : QWidget::render() paint to painter with wrong offset.



m.p
24th February 2013, 14:23
Hi,

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

This is a paint method of the delegate:

void PreviewWidget::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
painter->save();

QBrush brush;
int r = ((double)qrand()/ static_cast<double>( RAND_MAX )) * 255;
int g= ((double)qrand()/ static_cast<double>( RAND_MAX )) * 255;
int b = ((double)qrand()/ static_cast<double>( RAND_MAX )) * 255;
brush.setColor(QColor(r,g,b));
brush.setStyle(Qt::SolidPattern);
painter->fillRect(option.rect, brush);

ImagePrev i;
i.render(painter, option.rect.topLeft());

painter->restore();
}


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

8764

Why this offset occur?

d_stranz
24th February 2013, 18:57
Why this offset occur?

Probably because you are passing option.rect.topLeft() into the render() call instead of (0,0).

m.p
24th February 2013, 19:04
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.

norobro
24th February 2013, 20:24
Here's a bug report on this: QTBUG-26694 (https://bugreports.qt-project.org/browse/QTBUG-26694)

Try the workaround in the attachment:
i.render( painter, painter->deviceTransform().map(option.rect.topLeft()));

m.p
25th February 2013, 14:40
Here's a bug report on this: QTBUG-26694 (https://bugreports.qt-project.org/browse/QTBUG-26694)

Try the workaround in the attachment:
i.render( painter, painter->deviceTransform().map(option.rect.topLeft()));

It works. Thanks a lot! :)