Skip to content

Instantly share code, notes, and snippets.

@QuantumCD
Last active December 8, 2018 11:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save QuantumCD/9863860 to your computer and use it in GitHub Desktop.
Save QuantumCD/9863860 to your computer and use it in GitHub Desktop.
Skeleton to switch a QMainWindow's palette, as well as all the child widgets' palette (can be used for dynamic theming)
// The button clicked events can be replaced with any slots, and/or you can define the
// switchPalette() to be a slot to use with QObject::connect()
// Dark Palette push button
void MainWindow::on_switchToDarkTheme_clicked()
{
this->switchPalette(darkPalette);
}
// Light palette push button
void MainWindow::on_switchToLightTheme_clicked()
{
this->switchPalette(lightPalette);
}
void MainWindow::switchPalette(const QPalette& palette)
{
this->setPalette(palette);
QList<QWidget*> widgets = this->findChildren<QWidget*>();
foreach (QWidget* w, widgets)
{
w->setPalette(palette);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment