I have C++ Qt 5.2.1 based project and one of graphic component looks like:
+-- ParentWidget == QScrollArea ----------+
| |^|
| +-- MiddleWidget == QWidget -------+ | |
| | (>) Expand/collapse button | | |
| |----------------------------------| | |
| | | | |
| | +-- ChildWidget ---------------+ | | |
| | | ... | | |0|
| | +------------------------------+ | |0|
| | | |0|
| | +-- ChildWidget ---------------+ | |0|
| | | ... | | |0|
| | +------------------------------+ | |0|
| | | |0|
| | ... | |0|
| +----------------------------------+ |0|
| |0|
| +-- MiddleWidget == QWidget -------+ |0|
| | (>) Expand/collapse button | | |
| |----------------------------------| | |
| | | | |
| | +-- ChildWidget ---------------+ | | |
| | | ... | | | |
| | +------------------------------+ | | |
| | | | |
| | +-- ChildWidget ---------------+ | | |
| | | ... | | | |
+-----------------------------------------+
So, ParentWidget is QScrollArea, others are QWidgets. ParentWidget contains some MiddleWidgets, MiddleWidget contains some ChildWidgets. MiddleWidget also can be expanded or collapsed - when collapsed, ChildWidgets are not "visible" (isVisible() == false).
When user scrolls ParentWidget up and down, ChildWidgets which are "visible" (isVisible() == true) can actually be not real visible on screen. ChildWidgets contains pictures, and it's inefficiently to store all pictures in memory. So i want to load picture when ChildWidget is really appears on screen and user sees it; when ChildWidget disappears, picture should be stored to file and erased from memory.
Right now i can check such real visibility changing via ParentWidget:
bool ParentWidget::eventFilter(QObject *object, QEvent *event)
{
if (object == widget() && event->type() == QEvent::Paint) {
for (auto middle : _middles) {
for (auto child : middle->childs()) {
if (!child->visibleRegion().isEmpty()) {
...
}
}
}
}
return QScrollArea::eventFilter(object, event);
}
But this way obviously has its disadvantages:
- ChildWidgets don't know about change their real visibility and depends on ParentWidget
- when ParentWidget's paint event occurs, all ChildWidgets should be checked
So my question is: can QWidget catch changing its real visibility state itself? If no, maybe there is more effective way to implement such behaviour?