0

I have set a dark theme on my QWidget. In that widget I call a QDialog. The dialog appears in default palette which is light.

This is my custom dialog (Which in this case is for choosing colors for palette! Twisted right?) .cpp file:

#include "custompalettedialog.h"

CustomPaletteDialog::CustomPaletteDialog(QPalette palette, QWidget *parent) : QDialog(parent),
    m_palette(palette)
{
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(0);
    mainLayout->setMargin(0);
    QFormLayout* mainFormLayout = new QFormLayout;
    mainFormLayout->setHorizontalSpacing(0);
    mainFormLayout->setVerticalSpacing(0);
    mainLayout->addLayout(mainFormLayout);

    for (int var = 0; var < QPalette::ColorRole::NColorRoles; ++var) {
        QPushButton* bandButton = new QPushButton;
        bandButton->setFlat(true);
        bandButton->setAutoFillBackground(true);
        QPalette pal = bandButton->palette();
        pal.setColor(QPalette::Button, m_palette.color(static_cast<QPalette::ColorRole>(var)));
        bandButton->setPalette(pal);
        connect(bandButton, &QPushButton::clicked, this, [this, bandButton, var]() {
            QColor color = QColorDialog::getColor();
            if (color.isValid()) {
                m_palette.setColor(static_cast<QPalette::ColorRole>(var), color);
                QPalette pal = bandButton->palette();
                pal.setColor(QPalette::Button, color);
                bandButton->setPalette(pal);
            }
        });
        mainFormLayout->addRow(QVariant::fromValue(static_cast<QPalette::ColorRole>(var)).toString(), bandButton);
    }

    QPushButton* doneButton = new QPushButton("Done");
    connect(doneButton, &QPushButton::clicked, this, &QDialog::accept);
    mainLayout->addWidget(doneButton);
}

QPalette CustomPaletteDialog::getPalette()
{
    return m_palette;
}

QPalette CustomPaletteDialog::getPalette(bool* ok, const QPalette palette, QWidget *parent, const QString title)
{
    CustomPaletteDialog* dialog = new CustomPaletteDialog(palette, parent);
    dialog->setFont(parent->font());
    dialog->setModal(true);
    dialog->setWindowTitle(title);
    dialog->exec();
    QPalette pal = dialog->getPalette();
    *ok = dialog->result();
    dialog->deleteLater();
    return pal;
}

and the .h file:

#ifndef CUSTOMPALETTEDIALOG_H
#define CUSTOMPALETTEDIALOG_H

#include <QDialog>
#include <QFormLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QColorDialog>
#include <QComboBox>

class CustomPaletteDialog : public QDialog
{
    Q_OBJECT
public:
    explicit CustomPaletteDialog(QPalette palette, QWidget *parent = nullptr);
    static QPalette getPalette(bool* ok, const QPalette palette, QWidget *parent = nullptr, const QString title = QString());
    QPalette getPalette();
private:
    QPalette m_palette;
};

#endif // CUSTOMPALETTEDIALOG_H

I call the static method getPalette from a parent widget. I have tried adding this:

dialog->setPalette(parent->palette());

before dialog->setFont(parent->font()); in that static method and not working. My question is how to propagate QPalette from parent QWidget to child QDialog?

This is how I used it:

QPushButton* paletteAddButton = new QPushButton("Add...");
mainGroupBoxLayout->addWidget(paletteAddButton);
connect(paletteAddButton, &QPushButton::clicked, this, [this, customThemeItemModel]() {
    bool ok = false;
    QPalette palette = CustomPaletteDialog::getPalette(&ok, this->palette(), this, "Chosse UI Palette");
    if (ok) {
        QStandardItem* item = new QStandardItem("newPalette");
        item->setToolTip("Double Click to Change Name, Right Click for More");
        item->setData(palette);
        customThemeItemModel->appendRow(item);
        saveThemes(customThemeItemModel);
    }
});

I have a list of themes, user can add custom themes to that list.

Mohammad Rahimi
  • 965
  • 5
  • 15
  • styles do not use the palette for all drawing, for example if they make use of native theme engines, better you make theme with changing styleSheet – Adib Aug 03 '21 at 09:46
  • I was setting my palette for the entire UI with `QMainWindow::setPalette`. I used `qApp->setPalette` and now `QDialog`s are following the UI theme. Still, I don't understand why a `QDialog` doesn't follow its parent. – Mohammad Rahimi Aug 04 '21 at 08:10

0 Answers0