Anti Earth
Anti Earth

Reputation: 4811

Qt - Find 'Actual Widget' underneath 'viewport' at Coordinates

My Qt program uses a system of retrieving widget objectNames when clicked, by finding the coordinates of mousePressEvents and then finding the child widget (or the Window) at those coordinates.

This works perfectly for most widgets, but does not for the following widgets;
QScrollBox, QTableWidget, QGraphicsView, QTextEdit, QSpinBox.

These widgets (except QSpinBox) return 'qt_scrollarea_viewport' instead of their objectName, presumably because that is a child widget of those listed above.

Eg:
You have a QTextEdit with geometry (0,0,50,50).
Obviously the coordinates (10,10) are 'on' this widget.
Yet, calling parent.childAt( (10,10) ) does not return the QTextEdit.
Calling .objectName() shows that this instead is a 'qt_scrollarea_viewport'.

If you had twenty different QTextEdits with twenty different object names, they'd all still return 'qt_scrollarea_viewport'.

So, how do I retrieve the actual objectName of the QTextEdit, and not it's graphical child widget (which is what I interpret a 'viewport' to be), given I have the coordinates of the widget?

Thanks!


PyQt4
Python 2.7.2
Windows 7

Upvotes: 2

Views: 1832

Answers (2)

alexisdm
alexisdm

Reputation: 29886

There can be several levels of widgets, and in that case, childAt will return the innermost widget.

So, to get the first level widget, you can loop until parent is the actual parent:

widget = parent.childAt(x,y)
while parent != widget.parent():
    widget = widget.parent()
actual_obj = widget.objectName()

Upvotes: 0

Anti Earth
Anti Earth

Reputation: 4811

It is a child widget.

Solution:

if widget.objectName() == 'qt_scrollarea_viewport':    
    actual_obj = widget.parent().objectName()

Upvotes: 2

Related Questions