Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt如何使用x()和y()位置在小部件上查找对象_Qt - Fatal编程技术网

Qt如何使用x()和y()位置在小部件上查找对象

Qt如何使用x()和y()位置在小部件上查找对象,qt,Qt,我有一个主窗口。在主窗口上,我有多个Qlabel。现在,我需要找到QLabel。 使用MousePressEvent,我可以获得单击鼠标的X()和Y()位置 如何使用此坐标来标识QLabel QT中是否有任何函数可以使用X()和Y()坐标查找单击的对象???使用QApplication中的widgetAt函数 QWidget *widget = qApp->widgetAt(x,y); 然后,您可以将其dynamic\u cast转换为QLabel而不是试图从鼠标坐标中识别单击了哪个标签

我有一个主窗口。在主窗口上,我有多个Qlabel。现在,我需要找到QLabel。 使用MousePressEvent,我可以获得单击鼠标的X()和Y()位置

如何使用此坐标来标识QLabel


QT中是否有任何函数可以使用X()和Y()坐标查找单击的对象???

使用
QApplication中的
widgetAt
函数

QWidget *widget = qApp->widgetAt(x,y);

然后,您可以将其
dynamic\u cast
转换为
QLabel

而不是试图从鼠标坐标中识别单击了哪个标签,您也可以使用标签的方法


例如,创建自己的重载标签类,并在
mousePressEvent()
上发出
clicked()
信号,然后可以将该信号绑定到插槽

因为QLabel是QWidget的一个子类,所以可以在QLabel::mousePressEvent中处理鼠标按下事件

virtual void mousePressEvent ( QMouseEvent * ev )
但是在QMainWindow中,您可以使用childAt在x,y处获取子窗口小部件

QWidget * QWidget::childAt ( int x, int y ) const

QLabel* label= static_cast<QLabel*>(mainWindow->childAt(x,y));
QWidget*QWidget::childAt(intx,inty)const
QLabel*label=static_cast(主窗口->childAt(x,y));
更多信息请访问:

在Qt5中,这同样有效

QTabBar *widget =(QTabBar*) qApp->widgetAt(QCursor::pos());

谢谢你,我试着用你的方式。它给出了地址作为回报。表示在调试模式下,标签显示地址“@819d4e8”。现在,如何使用此地址标识单击的标签。请为我提供更多的指南。@skg您得到的是指向标签的指针,因此您不需要做任何其他事情,您可以在此指针上调用函数<代码>QLabel*label=static_cast(主窗口->childAt(x,y));label->setText(“我就是您单击的标签”)谢谢伊斯梅尔,你能再给我一点建议吗??我试过这个,每次对任何标签都给出“0x0”。对QCursor::pos()来说+1似乎有效,而QMouseEvent::pos()对我不起作用。唯一不严格匹配的是,我的意思是你可以得到任何QWidget指针使用qbject_cast获得更好的结果