QT中 鼠标点击图片中位置 和 控件的几何位置不一致

问题描述: 创建了一个mainwindow类型的程序 ,其上 放置了两个label控件AB,点击控件左上角位置时,发现控件位置和实际位置的X向一致,Y向始终有15左右的误差。

因为界面上可能同时有状态栏,菜单栏等,而我们放控件的地方是在主窗口centralWidget 这个容器中,所以,控件AB的相对于主窗口的位置应该为ui->label->pos() + ui->centralWidget->pos()

ui布局图

在这里插入图片描述

实际的ui布局图,我并没有给整体进行布局

需求分析

例子:下面有两幅图像,第二幅图中A点位于 控件中心位置,点击A点时,界面调试信息输出320240附近【将缩略图图像上的信息还原到原来图像中的位置,但由于像素点比较小,可以有点误差】。控件大小为400300,图像大小为640*480
在这里插入图片描述

void MainWindow::on_pushButton_2_clicked()//处理后图像//显示图像
{
//    QPixmap pix("E:/wokspace/suning/designer_first/tesst/sn-jcw/image/202081017322.png");
//    ui->label_2->setScaledContents(true);
//    ui->label_2->setPixmap(pix);
    //如果要处理像素,使用QImage
    QImage image("E:/wokspace/suning/designer_first/tesst/sn-jcw/image/202081017322.png");
    ui->label_2->setScaledContents(true);
    ui->label_2->setAlignment(Qt::AlignHCenter);//水平居中
    //将中间位置的像素点画黑点

    for(int i = image.width() / 2 - 3; i <= image.width() / 2; i++)
    {
        image.setPixel(i, image.height() / 2, 0);
    }
    ui->label_2->setPixmap(QPixmap::fromImage(image));


}

//鼠标释放事件
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    QPoint pt;
    pt.setX(event->pos().x() - (ui->label_2->geometry().x() + ui->centralWidget->pos().x()));//鼠标相对于控件label_2的位置
    pt.setY(event->pos().y() - (ui->label_2->geometry().y() + ui->centralWidget->pos().y()));//考虑到主窗口的问题
    qDebug()<<"new pos = "<<pt.x()<<pt.y()<<"mouse pos"<<event->pos().x()<<event->pos().y()<<"gemo pos"<<ui->label_2->geometry().x()<<ui->label_2->geometry().y();


    QPoint newpt;
    double nWidth = 640;
    double nHeight = 480;
    double nLabWidth = 400;
    double nLabHeig = 300;
    double xRat = nWidth / nLabWidth;//X向缩放比例
    double yRat = nHeight / nLabHeig;
    newpt.setX(int(pt.x() * xRat));//原始图像中位置
    newpt.setY(int(pt.y() * yRat));
    qDebug()<<"newpt pos = "<<newpt.x()<<newpt.y()<<"Ration"<<xRat<<yRat<<"x==="<<event->x()<<event->y();


    qDebug()<<ui->label_2->indent()<<ui->label_2->margin()<<ui->centralWidget->pos().x()<<ui->centralWidget->pos().y()<<ui->label_2->pos().x()<<ui->label_2->pos().y();
}

界面调试信息:

点击A点时

new pos =  199 150 mouse pos 219 606 gemo pos 20 440
newpt pos =  318 240 Ration 1.6 1.6 x=== 219 606
-1 0 0 16 20 440

点击label2控件左上角时

new pos =  0 0 mouse pos 20 456 gemo pos 20 440
newpt pos =  0 0 Ration 1.6 1.6 x=== 20 456
-1 0 0 16 20 440

总结

  1. ui->centralWidget->pos() 为(0,16)

  2. 控件2的实际位置为:ui->label_2->geometry().x() + ui->centralWidget->pos().x()

  3. 将缩略图中图像中某点 换算到原始图像中时,使用一定的缩放比例进行处理。

  4. 注意理解,主窗口,而pos获得的是相对父窗口的相对位置

  5. 同时,ui->label->pos() 与 ui->label->geometry()结果是一样的

  6. 获得鼠标相对于控件2的位置上,这样好转换。pt.setX(event->pos().x() - (ui->label_2->geometry().x() + ui->centralWidget->pos().x()));

特别说明:

对于QWdiget类型的,没有ui->centralWidget 这个,不需要设置了。
QMainWindow的布局如下图所示
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

鼠标位置event->globalPos()与pos()函数

globalPos() 是全局坐标,基准是电脑屏幕左上角。
pos()的基准是 父窗体的左上角。

参考文章
如何获得 Qt窗口部件在主窗口中的位置–确定鼠标是否在某一控件上与在控件上的位置
https://www.cnblogs.com/findumars/p/8001411.html
Qt获取鼠标位置(绝对位置、相对位置)
https://blog.csdn.net/owen7500/article/details/51035078

Qt学习笔记2:QMainWindow和QWidget的区别
https://blog.csdn.net/superhcq/article/details/53509183?utm_source=blogxgwz6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值