gpt4 book ai didi

c++ - 找到 QGraphicsScene 的中心位置?

转载 作者:行者123 更新时间:2023-11-30 00:49:57 26 4
gpt4 key购买 nike

我尝试使用以下代码向我的场景添加一行:

    line = new QGraphicsLineItem(200,55,200,55);
mscene.addItem(ruler);

但是 QGraphicsLineItem 的坐标似乎从左下角开始。我不想以任何方式改变原点。我只想检索 qgraphicsscene/qgraphicsview 的中心位置坐标。我应该使用什么功能?

最佳答案

line = new QGraphicsLineItem(200,55,200,55);
mscene.addItem(ruler);

作为docs QGraphicsLineItem 的这个构造函数的状态:-

Constructs a QGraphicsLineItem, using the line between (x1, y1) and (x2, y2) as the default line.

这将创建一个 QGraphicsLineItem,其局部坐标 为 (200, 55, 200, 55),因此您创建的线条的起点和终点坐标位于同一点 (200, 55 ).

创建线对象并将其添加到场景后,您可以通过调用 setPos 来设置它的位置。 .要获取场景的中心位置,可以使用 QGraphicsScene::width()QGraphicsScene::height() :-

// Assuming pScene is a pointer to the QGraphicsScene
line->setPos(pScene->width()/2, pScene->height()/2);

QGraphicsView 只是一个查看场景的窗口,因此它可以比场景小。在这种情况下,项目可能位于场景的中心,但不会出现在 View 的中心。

您可以通过调用 QGraphicsView::centerOn(const QGraphicsItem * item) 将 View 集中在一个项目上

关于c++ - 找到 QGraphicsScene 的中心位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26602264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com