英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论