看起來像你必須旋轉自己的。以下是我們解決方案的重要部分。如果有人這麼傾向,這可能會泛化。由於我們的業務規則可能會破壞其他應用程序,因此我也可以假設某些事情。肘節是可在編譯時定義宏:YOUR_APP_APPLY_PREFERENCES_IMMEDIATELY
preferences_dialog.h
class PreferencesDialog : public QDialog {
Q_OBJECT
public:
explicit PreferencesDialog(... various arguments ...,
QWidget *parent);
private slots:
void ModifyMapLanguages();
private:
void ApplyLanguageChanges(const QList<Language> &languages,
const QHash<LanguageKey, LanguageKey> &renames);
void Initialize();
#ifndef YOUR_APP_APPLY_PREFERENCES_IMMEDIATELY
public slots:
void accept();
private slots:
void ApplyDialog();
private:
QList<Language> pending_languages_;
QHash<LanguageKey, LanguageKey> pending_language_renames_;
#endif
};
preferences_dialog.cpp
#include "forms/preferences_dialog.h"
#include "ui_preferences_dialog.h"
PreferencesDialog::PreferencesDialog(... various arguments ...,
QWidget *parent) :
QDialog(parent),
... various initializers ... {
ui->setupUi(this);
Initialize();
}
void PreferencesDialog::ApplyLanguageChanges(
const QList<Language> &languages,
const QHash<LanguageKey, LanguageKey> &renames) {
// Do the actual changes here, whether immediate or postponed
}
void PreferencesDialog::Initialize() {
// Disable the minimize and maximize buttons.
Qt::WindowFlags flags = this->windowFlags();
flags |= Qt::CustomizeWindowHint;
flags &= ~Qt::WindowMinMaxButtonsHint;
setWindowFlags(flags);
// buttons is the QDialogButtonBox with Ok, Cancel, and Apply buttons
#ifdef YOUR_APP_APPLY_PREFERENCES_IMMEDIATELY
ui->buttons->setVisible(false);
#else
QPushButton *apply_button = ui->buttons->button(QDialogButtonBox::Apply);
connect(apply_button, SIGNAL(clicked()), SLOT(ApplyDialog()));
#endif
}
void PreferencesDialog::ModifyMapLanguages() {
// Get the changes; in my case, they are coming from a dialog wizard
LanguageSetupWizard wizard(map_->languages(), true, this);
wizard.setWindowModality(Qt::WindowModal);
if (QDialog::Accepted == wizard.exec()) {
#ifdef YOUR_APP_APPLY_PREFERENCES_IMMEDIATELY
ApplyLanguageChanges(wizard.languages(), wizard.language_renames());
#else
pending_languages_ = wizard.languages();
pending_language_renames_ = wizard.language_renames();
#endif
}
}
#ifndef YOUR_APP_APPLY_PREFERENCES_IMMEDIATELY
void PreferencesDialog::ApplyDialog() {
if (!pending_languages_.isEmpty()) {
ApplyLanguageChanges(pending_languages_, pending_language_renames_);
}
}
void PreferencesDialog::accept() {
ApplyDialog();
QDialog::accept();
}
#endif