@Wieland Hi, thanks for the reply, if you subclass QGraphicsView like this:
#ifndef MYVIEW_H
#define MYVIEW_H
#include <QGraphicsView>
#include "MyItem.h"
class MyView : public QGraphicsView
{
Q_OBJECT
public:
inline explicit MyView(QWidget * parent = 0) :
QGraphicsView(parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
public:
inline virtual void resizeEvent(QResizeEvent *)
{
QSize size = contentsRect().size();
static_cast<MyItem *>(scene()->items().at(0))->parViewContRect = QRect(QPoint(), size);
invalidateScene();
viewport()->update();
}
};
#endif // MYVIEW_H
Here is the result: http://pasteboard.co/wwmZHz6.png
There is a 1px offset but on the bottom-right side.
If you comment out this line:
ui->graphicsView->setAlignment( Qt::AlignLeft | Qt::AlignTop );
Then the item is offset by (in my case) 92x97 px.
But then if I add this to the resizeEvent method:
scene()->items().at(0)->setPos(-1, -1);
the item will fill the whole view.
However it doesn't solve my problem. I added this to my original code and it didn't fix the problem. But I have noticed that if I disable cache on all of my QGraphicsItems, then it works. I use
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
on QGraphicsItem by default, then I change the bounding box like this:
// Parameter canvasSize is equal to QGraphicsView::contentsRect().size()
void Layer::updateViewport(const QSize & canvasSize) {
if (cacheMode() != QGraphicsItem::NoCache)
prepareGeometryChange();
mBoundingBox = QRect(QPoint(), canvasSize);
if (cacheMode() != QGraphicsItem::NoCache)
update();
}
Here is the result: http://pasteboard.co/wzteGW3.png
This happens when the cache is set to QGraphicsItem::DeviceCoordinateCache (or QGraphicsItem::ItemCoordinateCache) and I quickly resize the window. Here is a Qt project which can reproduce the error: http://matejtomcik.com/ViewProblem.zip