自己关于Qt坐标的错误使用

今天在公司做项目的时候,对转换坐标系出现了一些差解。

我建的窗口是一个QWidget   A 里面有两个QLabel,

另有一个跟Label同样大小的窗口控件B;

B的状态窗口C。

由B生成的对象分别显示到A的两个Label中。 在我显示C时 我打算显示到B的边缘,当鼠标移动到B上时显示出来。


在显示C时 因为A无法显示完,因此需要绝对坐标来放置。(在B中)

第一次犯错  mapToGlobal(   mapToParent(this->pos() )  ) 来获取最终坐标                                ///  this->pos() 就是获取当前模块在父窗口中的坐标

第二次犯错 发现后  修改为 mapToGlobal( this ->pos()  )///  发现坐标转换后叠加了窗口中的坐标    坐标不准确 偏差为相对于客户区的坐标   


错误的原因是 此函数是基于当前模块转换,  以上函数都是当前模块(B)的成员函数,但我当做全局函数来用。

正确获取   直接使用 mapToGlobal(QPoint(0, 0)) 就行了;



个人错误 仅供参考

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本资源为Qt绘图基础,世界坐标系转换为逻辑坐标系。世界坐标系原点在视图左上角,本例子通过世界坐标转换,将坐标原点定位在视图中央,Y轴向上,X轴向右,并绘制坐标轴,基于逻辑坐标系下的绘图,可将转换关系函数取消生效,对比世界坐标系下的绘图。 重写PainterEvent函数: void QtPixPainter::paintEvent(QPaintEvent* event) { QPainter painter(this); // 反走样 painter.setRenderHint(QPainter::Antialiasing, true); //物理坐标系与逻辑坐标系的转换,如果不转换,下面的绘图都是在世界坐标系下 setWorldTransform(painter); // 其他一些绘制矩形,多边形的例子,经过上面转换,都是在逻辑坐标系下 drawRectScale(painter); //draw_shearRect(painter); //利用rotate()函数进行比例变换,实现缩放效果 //draw_rotate_act(painter); //draw_by_save_restore(painter); //transform_draw_SinX(painter); transform_draw(painter); local_drawConvexPolygon(painter); } // 将世界坐标(原点左上角)转换为逻辑坐标(原点在屏幕中间) QPointF QtPixPainter::mapToScene(const QPointF& point) { QTransform transMatrix = _transform.inverted(); //翻转矩阵? return transMatrix.map(point); //将点piont映射到transMatrix定义的坐标系中来 } // 将鼠标的逻辑位置返回并以标签形式展示 void QtPixPainter::mouseMoveEvent(QMouseEvent* event) { QString msg; QPointF mouse_po = mapToScene(event->pos()); //总是返回屏幕物理坐标系 double x = mouse_po.x(); // 总是返回屏幕物理坐标系 double y = mouse_po.y(); QString str = "(" + QString::number(x) + "," + QString::number(y) + ")"; //qDebug()<<"world x = "<pos().x()<<",world y = "<pos().y(); m_mouse_lable->setText(str); }
好的,我可以帮您解答这个问题。以下是一个简单的Qt程序,用于比较两个坐标文档的误差: ```C++ #include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow> #include <QtWidgets/QLabel> #include <QtWidgets/QLineEdit> #include <QtWidgets/QPushButton> #include <QtWidgets/QFileDialog> #include <QtCore/QDebug> #include <QtCore/QFile> #include <QtCore/QTextStream> #include <cmath> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { // 设置窗口标题和大小 setWindowTitle(tr("比较坐标文档")); setFixedSize(400, 200); // 创建两个标签和四个文本框 QLabel *label1 = new QLabel(tr("文档1路径:"), this); label1->setGeometry(30, 30, 120, 30); QLabel *label2 = new QLabel(tr("文档2路径:"), this); label2->setGeometry(30, 70, 120, 30); QLineEdit *edit1 = new QLineEdit(this); edit1->setGeometry(150, 30, 180, 30); QLineEdit *edit2 = new QLineEdit(this); edit2->setGeometry(150, 70, 180, 30); // 创建两个按钮,并添加点击事件 QPushButton *button1 = new QPushButton(tr("选择文档1"), this); button1->setGeometry(340, 30, 50, 30); connect(button1, &QPushButton::clicked, [this, edit1]() { QString fileName = QFileDialog::getOpenFileName(this, tr("选择文档1"), "", tr("文本文件 (*.txt)")); if (!fileName.isEmpty()) { edit1->setText(fileName); } }); QPushButton *button2 = new QPushButton(tr("选择文档2"), this); button2->setGeometry(340, 70, 50, 30); connect(button2, &QPushButton::clicked, [this, edit2]() { QString fileName = QFileDialog::getOpenFileName(this, tr("选择文档2"), "", tr("文本文件 (*.txt)")); if (!fileName.isEmpty()) { edit2->setText(fileName); } }); // 创建一个按钮,并添加点击事件 QPushButton *button = new QPushButton(tr("比较文档"), this); button->setGeometry(150, 120, 100, 30); connect(button, &QPushButton::clicked, [this, edit1, edit2]() { // 打开两个文档,并读取坐标数据 QFile file1(edit1->text()); if (!file1.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "无法打开文档1:" << file1.errorString(); return; } QTextStream in1(&file1); QList<QPointF> points1; while (!in1.atEnd()) { QString line = in1.readLine(); QStringList fields = line.split(","); if (fields.count() != 2) { qDebug() << "文档1格式错误:" << line; continue; } double x = fields[0].toDouble(); double y = fields[1].toDouble(); points1.append(QPointF(x, y)); } file1.close(); QFile file2(edit2->text()); if (!file2.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "无法打开文档2:" << file2.errorString(); return; } QTextStream in2(&file2); QList<QPointF> points2; while (!in2.atEnd()) { QString line = in2.readLine(); QStringList fields = line.split(","); if (fields.count() != 2) { qDebug() << "文档2格式错误:" << line; continue; } double x = fields[0].toDouble(); double y = fields[1].toDouble(); points2.append(QPointF(x, y)); } file2.close(); // 计算坐标间的误差 double totalError = 0; for (int i = 0; i < points1.count() && i < points2.count(); i++) { double error = std::sqrt(std::pow(points1[i].x() - points2[i].x(), 2) + std::pow(points1[i].y() - points2[i].y(), 2)); totalError += error; } // 显示误差 QString text = tr("两个文档之间的总误差为:") + QString::number(totalError); QLabel *resultLabel = new QLabel(text, this); resultLabel->setGeometry(30, 160, 300, 30); }); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } #include "main.moc" ``` 这个程序创建了一个窗口,其中包含两个标签和四个文本框,以及两个选择文件的按钮和一个比较文档的按钮。当用户点击选择文件按钮时,程序将打开一个文件对话框,让用户选择一个文档。当用户点击比较文档按钮时,程序将从两个文档中读取坐标数据,计算它们之间的误差,并显示误差。 注意:这个程序只是一个简单的示例,实际应用中还需要添加错误处理、文件格式检查等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值