用qt实现标签打印设计软件,保存画出的标签时,标签的坐标会偏移
时间: 2024-02-03 17:11:18 浏览: 40
标签的坐标偏移可能是因为在保存标签时,坐标系的原点位置发生了变化。一种解决方法是在保存标签时,将标签的坐标转换为绝对坐标,即以屏幕左上角为坐标原点。
代码示例:
```
QPoint labelPos = label->pos(); // 获取标签相对于父控件的坐标
QPoint screenPos = mapToGlobal(labelPos); // 将坐标转换为屏幕坐标系下的坐标
QPoint absPos = screenPos - QApplication::desktop()->geometry().topLeft(); // 将坐标转换为绝对坐标,即以屏幕左上角为原点
label->setPos(absPos); // 设置标签的绝对坐标
```
在加载标签时,也需要将标签的坐标转换为相对于父控件的坐标。
代码示例:
```
QPoint absPos = labelObj["pos"].toPoint(); // 获取标签的绝对坐标
QPoint screenPos = absPos + QApplication::desktop()->geometry().topLeft(); // 将坐标转换为屏幕坐标系下的坐标
QPoint labelPos = mapFromGlobal(screenPos); // 将坐标转换为相对于父控件的坐标
label->setPos(labelPos); // 设置标签相对于父控件的坐标
```
注意,在保存和加载标签时,都需要考虑标签所在的父控件的坐标系。如果父控件的坐标系也发生了变化,需要将坐标转换为相对于新的父控件的坐标。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)