Quick Tip: Quickly Access All QWidgets in a GUI

The other day, I was trying to change the color palette of all widgets in a GUI application to implement a theme change. I was having problems getting all the widgets to change their palette without having to destroy and re-create the MainWindow. Instead, I found a handy way to easily iterate over all your QWidgets. Enjoy!

        QList<QWidget*> widgets = this->findChildren<QWidget*>();

        foreach (QWidget *widget, widgets)

If you look closely, all we are doing is getting a container of QWidget pointers that contains everything inside the MainWindow (this). We can use the findChildren template function to get all the children that are QWidget pointers. Then we simply use the handy foreach loop to iterate over all these very easily.

Note, you can access all QWidget methods, so this could be very useful for other things besides setting the palette. One example that comes to me off the top of my head is accepting/denying drops. Very handy indeed. 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s