Unsolved Get visibility status of widget without inheritance?
As per the docs, the
.isVisible()method of a QWidget returns True "if all its parent widgets up to the window are visible".
Is there a way to determine whether a widget has a visible status itself only regardless of the state of its parents?
For example, let's say I've got a QGridLayout in a QFrame. On that layout are two widgets, widgetA and widgetB. Widget A is visible, but widget B is hidden. If I do
.setVisible(False)on the whole QFrame, then
widgetB.isVisible()will both return False. But if I re-show the frame, widgetA will re-appear, but widget B will remain hidden, remembering its previous state. Whilst the containing frame is hidden, is there any parameter or method which will return True on widget A but False on widget B?
Pl45m4 last edited by
is there any parameter or method which will return True on widget A but False on widget B?
Add your own variable to your widget class.
A function returning
widget Ais hidden because of the parent, would be a lie :)
I wondered if that might be the solution, but I was hoping it wouldn't be because in practice I've got a wide variety of different widgets in use, and subclassing everything (QLabel, QComboBox, QLineEdit, QCheckBox, QListWidget, QSpinBox and a few others) is going to take a bit of time.
I agree that
.visible()would be lying if it returned True.
However the Qt framework obviously remembers each widget's visibility status individually, because when the parent is set to visible, it doesn't change the previously set visibility flags on each widget. So I was hoping that existing boolean might have been exposed in a method like
.wouldBeVisibleIfParentWereVisible()(except better worded!)
Pl45m4 last edited by
However the Qt framework obviously remembers each widget's visibility status individually, because when the parent is set to visible, it doesn't change the previously set visibility flags on each widget. So I was hoping that existing boolean might have been exposed in a method like .wouldBeVisibleIfParentWereVisible() (except better worded!)
Calling setVisible(false) or hide() hides a widget explicitly. An explicitly hidden widget will never become visible, even if all its ancestors become visible, unless you show it.
JonB last edited by JonB
[Crazy name! :) ]
I don't think Qt offers you public access to the "visibility flag" which is (presumably) stored in each widget, only to the
isVisible()method which calculates visibility including parentage.
Returns true if this widget would become visible if ancestor is shown; otherwise returns false.
The true case occurs if neither the widget itself nor any parent up to but excluding ancestor has been explicitly hidden.
If you don't get a better answer, couldn't you use this to calculate what the widget's visibility must be by walking up ancestors?
For example (untested), if you have a
widgetwhich is really visible but its
parentWidget()is set to hidden, I would expect/hope:
widget.isVisible(); // false widget.parentWidget()->isVisible(); // false widget.isVisibleTo(widget.parentWidget()); // true
To get it right you'll have to do some sort of iteration/recursion up the ancestor tree, e.g. think about case where it is (only)
widget->parentWidget()->parentWidget()which has been explicitly hidden. EDIT Actually, if you read the docs carefully I think
widget.isVisibleTo(widget.parentWidget());always returns the widget's own visibility regardless of parent or its ancestors' states. So need to walk the tree --- test with grandparent case.
While I was writing this you posted with
.wouldBeVisibleIfParentWereVisible()in it. That's exactly what
Thanks very much for the response.
You're absolutely right- I'd managed to overlook
isVisibleTo()by misunderstanding the docs and thinking it was for a different purpose.
widgetA.isVisibleTo(widgetA.parentWidget())does work exactly as I wanted, returning the 'local' visibility relative to the frame that it's contained on, regardless of whether that frame or anything higher up the tree is hidden.
I can also refer to a specific higher-up widget in some cases too- the parameter must be a widget rather than a layout though.