坐标混乱

发布于 2024-09-06 01:17:38 字数 421 浏览 2 评论 0原文

我对 QGraphicsItem 进行了子类化并重新实现了绘制。
在绘画中,我写了类似这样的内容来标记该项目:

 painter->drawText("Test",10,40);

一段时间后,我认为处理单独项目的标签可能会很有用。所以我写了这样的东西。

QGraphicsTextItem *label = new QGraphicsTextItem("TEST",this);
setPos(10,40);

但是两个“TEST”绘图不会出现在屏幕上的同一位置。我猜差异可能与项目坐标 - 场景坐标有关。我尝试了 QGraphicsItem 接口内的所有 mapFrom... 和 mapTo... 组合,但没有任何进展。我希望绘图出现在屏幕上的同一位置。
我想念什么?

I subclassed QGraphicsItem and reimplemented paint.
In paint I wrote something like this for labeling the item:

 painter->drawText("Test",10,40);

After some time I think It may be useful to handle labeling with seperate item. So I wrote something like this.

QGraphicsTextItem *label = new QGraphicsTextItem("TEST",this);
setPos(10,40);

But two "TEST" drawing do not appear in the same place on screen. I guess difference may be related with item coordinates - scene coordinates. I tried all mapFrom... and mapTo... combinations inside QGraphicsItem interface but no progress. I want to drawings to appear in the same place on screen.
What I miss?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2024-09-13 01:17:38

我假设您在这两种情况下使用相同的字体大小和类型。如果位置差异非常小,原因可能是 QGraphicTextItem 对其包含的文本使用了一些填充。我会尝试使用 QGraphicsSimpleTextItem ,它不会在内部添加花哨的东西,看看您是否仍然遇到同样的问题。如果您使用画家或 setPost,坐标系是相同的,所以这不是问题。如果这没有帮助,我建议为两者指定相同的矩形,以避免 Qt 添加它自己的分隔空间。

I assume that you are using the same font size and type in both cases. If the difference in position is very small the reason can be the QGraphicTextItem is using some padding for the text it contains. I would try to use QGraphicsSimpleTextItem that is not going to add fancy stuff internally and see if you still have the same problem. The coordinates system is the same one if you use painter or setPost so that is not the problem. If this doesn't help I will suggest to specify the same rect for both to avoid Qt adding it owns separation spaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文