在Mac,Gnome,KDE和Windows上使用Qt的平台本机首选项对话框 [英] Platform-native preferences dialog with Qt on Mac, Gnome, KDE, and Windows

查看:123
本文介绍了在Mac,Gnome,KDE和Windows上使用Qt的平台本机首选项对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac和Gnome上,本机应用程序使用应用程序首选项对话框,该对话框会在选定设置后立即应用所选设置.在Windows和(我认为)KDE上,仅在按下应用"或确定"按钮时才应用首选项.

On Mac and Gnome, native applications use an application preferences dialog that immediately applies the chosen settings as they are selected. On Windows and (I think) KDE, preferences are applied only when an "Apply" or "OK" button is pressed.

是否有内置的Qt好东西可以为您执行此操作,或者您是否必须在对话框代码中包含多个#ifdef来处理此问题(Q_WS_WINQ_WS_MACQ_WS_X11)?

Are there any built-in Qt goodies to do this for you, or do you have to include several #ifdef's in the dialog code to handle this (Q_WS_WIN, Q_WS_MAC, Q_WS_X11)?

如果您以前曾经做过这样的事情(甚至使用#ifdef的话),您是否可以分享有关如何实现的基本代码?

If you have done something like this before (even using #ifdef's), could you share skeleton code as to how you pulled it off?

推荐答案

好像您必须旋转自己的.这是我们解决方案的重要组成部分.如果有人愿意,这可能可以一概而论.由于我们的业务规则可能会破坏其他应用程序,因此我也可以承担某些事情.切换是一个可以在编译时定义的宏:YOUR_APP_APPLY_PREFERENCES_IMMEDIATELY

Looks like you have to spin your own. Here are the important parts of our solution. This could probably be generalized if someone is so inclined. I'm also able to assume certain things because of our business rules that may break other applications. The toggle is a macro that can be defined at compile-time: 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

这篇关于在Mac,Gnome,KDE和Windows上使用Qt的平台本机首选项对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆