使用鼠标在PyQt中获取有关底层对象的信息。

huangapple go评论59阅读模式
英文:

Use mouse to get information about the underlying object in PyQt

问题

I am working with a preexisting Python Qt desktop application. I don't understand where some graphical elements come from. I would like to be able to obtain information about the object that created e.g. a widget or button by hovering or clicking my mouse. I want to be able to go from the graphics displayed to me by the application to the underlying object or class. Anything like a class name or a reference to the object would be useful. Is there a function to do that in PyQt?

我正在使用现有的Python Qt桌面应用程序。我不明白一些图形元素是从哪里来的。我希望能够通过悬停或点击鼠标来获取有关创建小部件或按钮的对象的信息。我希望能够从应用程序显示给我的图形转到底层对象或类。任何类似类名或对象引用的东西都会很有用。在PyQt中是否有这样的功能?

I tried googling this, but my results are completely overwhelmed by discussions of how to connect mouse callbacks to your widget, but my problem is trying to find a widget that I see, but don't know how to programmatically access.

我尝试在谷歌上搜索这个问题,但我的搜索结果完全被关于如何将鼠标回调与小部件连接的讨论所淹没,但我的问题是尝试找到一个我看到但不知道如何以编程方式访问的小部件。

英文:

I am working with a preexisting Python Qt desktop application. I don't understand where some graphical elements come from. I would like to be able to obtain information about the object that created e.g. a widget or button by hovering or clicking my mouse. I want to be able to go from the graphics displayed to me by the application to the underlying object or class. Anything like a class name or a reference to the object would be useful. Is there a function to do that in PyQt?

I tried googling this, but my results are completely overwhelmed by discussions of how to connect mouse callbacks to your widget, but my problem is trying to find a widget that I see, but don't know how to programmatically access.

答案1

得分: 0

from qtpy.QtWidgets import QApplication
from qtpy.QtGui import QCursor

app = QApplication.instance()
widget = app.widgetAt(QCursor.pos())

This will work for widgets, but may not work for custom drawn elements.

英文:
from qtpy.QtWidgets import QApplication
from qtpy.QtGui import QCursor

app = QApplication.instance()
widget = app.widgetAt(QCursor.pos())

This will work for widgets, but may not work for custom drawn elements.

huangapple
  • 本文由 发表于 2023年5月18日 04:55:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定