简体   繁体   English

坐标混乱

[英]Coordinate confusion

I subclassed QGraphicsItem and reimplemented paint. 我将QGraphicsItem子类化,并重新实现了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. 但是,两个“ TEST”图形未出现在屏幕上的同一位置。 I guess difference may be related with item coordinates - scene coordinates. 我想差异可能与项目坐标-场景坐标有关。 I tried all mapFrom... and mapTo... combinations inside QGraphicsItem interface but no progress. 我尝试了QGraphicsItem接口内的所有mapFrom ...和mapTo ...组合,但没有任何进展。 I want to drawings to appear in the same place on screen. 我希望图纸显示在屏幕上的同一位置。
What I miss? 我想念什么?

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. 如果位置差异很小,则可能是QGraphicTextItem对其中包含的文本使用了一些填充。 I would try to use QGraphicsSimpleTextItem that is not going to add fancy stuff internally and see if you still have the same problem. 我会尝试使用不会在内部添加奇特的东西的QGraphicsSimpleTextItem,看看您是否仍然遇到相同的问题。 The coordinates system is the same one if you use painter or setPost so that is not the problem. 如果使用painter或setPost,则坐标系是相同的,所以这不是问题。 If this doesn't help I will suggest to specify the same rect for both to avoid Qt adding it owns separation spaces. 如果这样做没有帮助,我建议为两者指定相同的rect,以避免Qt添加它拥有的分隔空间。

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

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