PDA

View Full Version : QGraphicsPixmapItem and QGraphicsView::itemAt() issue



alazar
28th March 2014, 14:23
Hi all,

I need to load georreferenced images in my program. To do it, I use GDAL library to obtain a QImage* and then, I create a QGraphicsPixmapItem from that image.

Because of the pixel resolution and to avoid resampling of the image, I have a subclass of QGraphicsPixmapItem where boundingRect() and paint() methods are reimplemented as follows:


QRectF CN_GraphicsOrtophotoItem::boundingRect() const
{
QSizeF size(pixmap().width()*m_xRes, pixmap().height()*m_yRes);
return QRectF(offset(), size);
}

void CN_GraphicsOrtophotoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawPixmap(boundingRect().toRect(), pixmap());
}



where m_xRes and m_yRes are the pixel resolutions (in m/pixel).

It works fine, but know I have a trouble: QGraphicsView::itemAt() doesn't work properly, because if the pixel resolution is 2 m/pixel (for example), then itemAt() method only works on the top left quarter of the image, and so on.

I thought itemAt() called boundingRect(), but now I'm not sure...:confused: Could anybody help me?

Thanks in advance.

alazar
31st March 2014, 09:59
OK, reimplementing the shape() function is the solution.