简体   繁体   中英

Qt GraphicsView stretch scene to fit

Ok, so I am using Qt and C++ as my environment.

I draw out a QGraphicsView in my UI.

I then create a scene and add lines to that scene. I run through an array of 5000 points and draw lines connecting each point.

QGraphicsScene *scene = new QGraphicsScene();
QPen pen2 = QPen(Qt::blue, 8.0);
int j=1;
for (int i=1; i<5000; i++)
{
    scene->addLine(xArray[i],yArray[i],xArray[j],yArray[j], pen2);
    j++;
}

The problem is that the numbers I am grabbing are very small, eg 2.000e-12. The numbers will consistently change based on the application. How can I adjust my scene to stretch to fill in my QGraphicsView . Now, all I see is a dot in the center of my view. Am I making sense?

使用sceneRect在QGraphicsScene中的QGraphicsView :: fitInView()会做缩放到适合你。

I never used it myself, but in Qt doc there is a

void QGraphicsView::scale ( qreal sx, qreal sy )

that allows you to scale your view in the x and y axis. If it is not sufficient, you could (if it is possible in your project) zoom manually when the values are too small or too big, when you draw your lines and apply this same factor to everything else. But if it is possible, I would go with the scale method included in Qt.

Hope this helps.

check whether this code helps. It just fits the image on GraphicsView

m_Scene.setSceneRect(m_QImage.rect());

m_Scene.addPixmap(QPixmap::fromImage(m_QImage,0));

m_GraphicsView.setScene(&m_Scene);

m_GraphicsView.fitInView(m_QImage.rect());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM