简体   繁体   English

Qt GraphicsView拉伸场景以适应

[英]Qt GraphicsView stretch scene to fit

Ok, so I am using Qt and C++ as my environment. 好的,所以我使用Qt和C ++作为我的环境。

I draw out a QGraphicsView in my UI. 我在我的UI中绘制了一个QGraphicsView

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. 我运行5000点的数组并画出连接每个点的线。

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. 问题是我抓到的数字非常小,例如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 . 如何调整我的场景以拉伸以填充我的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 我自己从未使用它,但在Qt doc中有一个

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

that allows you to scale your view in the x and y axis. 允许您在x和y轴上缩放视图。 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. 但是如果有可能的话,我会选择Qt中包含的比例法。

Hope this helps. 希望这可以帮助。

check whether this code helps. 检查此代码是否有帮助。 It just fits the image on GraphicsView 它只适合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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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