判断QWidget是否可见,调用isVisible()还是isHidden()?
在写这篇文章之前,我一直以为是一样的,也就是:
isVisible() == !isHidden()
No!No!No!................
看QT文档对isHidden()的说明:
bool QWidget::isHidden() const
Returns true if the widget is hidden, otherwise returns false.
A hidden widget will only become visible when show() is called on it. It will not be automatically shown when the parent is shown.
To check visibility, use !isVisible() instead (notice the exclamation mark).
isHidden() implies !isVisible(), but a widget can be not visible and not hidden at the same time. This is the case for widgets that are children of widgets that are not visible.
Widgets are hidden if:
- they were created as independent windows,
- they were created as children of visible widgets,
- hide() or setVisible(false) was called.
首先黑色粗体部分,QT明确了如果要检查控件的可见性,要用isVisible()!
其次红色粗体部分说明了原因,QWidget在某些情况下可能是作为其他控件的子控件,此种情况下,控件的可见性和父控件的可见性有关,也就说,如果父控件不可见,就算子控件自身不是隐藏的,也是不可见的。这时如果还使用isHidden()判断子控件的可见性,就不对了!
所以,没事,多翻翻文档!!!!!