Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
5.15.2, 6.2.2
-
None
Description
As QGraphicsView::fitInView doc said,it should scale properly and move item center on view port. But it moves small item far away from the viewport.
Test code :
void test(){ auto win = new QMainWindow; auto scene = new QGraphicsScene(win); scene->setSceneRect(QRectF(0, 0, 5000, 5000)); auto view = new QGraphicsView(scene); auto fitBtn = new QPushButton("FitInViewBtn"); auto layout = new QVBoxLayout; layout->addWidget(fitBtn); layout->addWidget(view); QWidget *widget = new QWidget; widget->setLayout(layout); win->setCentralWidget(widget); win->setGeometry(100, 100, 800, 600); win->show(); auto textItem = new QGraphicsTextItem("TestTextItem"); textItem->setFlag(QGraphicsItem::ItemIsMovable); textItem->setFlag(QGraphicsItem::ItemIsSelectable); textItem->setPos(view->mapToScene(view->viewport()->rect().center())); scene->addItem(textItem); // scale number to chang item view size,bigger number greater than 1E106 will move item far away from view port double scale = 1.0E7; fitBtn->connect(fitBtn,&QPushButton::clicked,[=](){ auto extent = textItem->sceneBoundingRect(); qDebug() << "before>> extent:" << extent << ",extentCenter:" << extent.center() << ",viewRect:" << view->viewport()->rect() << ",viewSceneCenter:" << view->mapToScene(view->viewport()->rect().center()) << ",view.m11:" << view->transform().m11() << ",item.m11:" << textItem->transform().m11(); textItem->setScale(1/scale); textItem->setScale(1/scale); extent = textItem->sceneBoundingRect(); view->fitInView(extent,Qt::KeepAspectRatio); qDebug() << "after>> extent:" << extent << ",extentCenter:" << extent.center() << ",viewRect:" << view->viewport()->rect() << ",viewSceneCenter:" << view->mapToScene(view->viewport()->rect().center()) << ",view.m11:" << view->transform().m11() << ",item.m11:" << textItem->transform().m11(); }); } int main(int argc, char *argv[]) { QApplication a(argc, argv); test(); return a.exec(); }
The bug will appear after click the "FitInViewBtn" button which is on the top of the app window.
Moreover, make calling view.centerOn(textItem.boundingRect().center()) after view.scale(1.1) as one operation to keep textItem on view center. Repeat the operation serval times until the total scale of the view much bigger,then textItem moved far away from view center.