在 Mac 和 Gnome 上, native 应用程序使用应用程序首选项对话框,该对话框会在选择时立即应用所选设置。在 Windows 和(我认为)KDE 上,仅当按下“应用”或“确定”按钮时才会应用首选项。
是否有任何内置的 Qt 好东西可以为您做这件事,或者您是否必须在对话框代码中包含几个 #ifdef
来处理这个 (Q_WS_WIN
, Q_WS_MAC
, Q_WS_X11
)?
如果您以前做过类似的事情(甚至使用 #ifdef
's),您能否分享关于如何完成它的框架代码?
最佳答案
看来你必须自己旋转。以下是我们解决方案的重要部分。如果有人愿意,这可能会被推广。由于我们的业务规则可能会破坏其他应用程序,我还可以假设某些事情。切换是一个可以在编译时定义的宏: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
关于macos - 在 Mac、Gnome、KDE 和 Windows 上使用 Qt 的平台原生首选项对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3397177/