Qt: How to determine whether a widget is visible or not in the QScrollArea?

Nyaruko picture Nyaruko · Feb 24, 2015 · Viewed 7.6k times · Source

Someone suggested that I re-implement the QWheelEvent handler and check each child widgets' visibleRegion is 0 or not.

Are there any better suggestions?

Answer

davepmiller picture davepmiller · Feb 24, 2015

When you add the widget. Give it a name.

QWidget* myWidget = new QWidget;
myWidget->setObjectName( "myWidget" );
...
//create scroll area
//add a layout to the scroll area
...
scrollArea->layout()->addWidget( myWidget );

Then, check visibility like so:

QWidget* widget = scrollArea->findChild< QWidget* >( "myWidget" );
std::cout << widget->isVisible() << std::endl;

You could keep a list of your widget names to more easily loop through and check when you're ready.