【问题标题】:Position of the "Apply" button in QDialogButtonBoxQDialogBu​​ttonBox 中“应用”按钮的位置
【发布时间】:2016-03-10 18:59:02
【问题描述】:

我想要一个QDialogButtonBox,按此特定顺序包含三个按钮:

Ok | Apply | Cancel

是否可以重新排列按钮以将Apply 置于中心位置?

【问题讨论】:

    标签: qt


    【解决方案1】:

    按钮布局是特定于平台的。

    Windows - Ok | Cancel | Apply
    OS X - Apply | Cancel | Ok
    KDE - Ok | Apply | Cancel
    GNOME - Apply | Cancel | Ok
    

    有两种方法可以强制使用非标准布局。

    您可以继承QProxyStyle 并重新实现styleHint 方法,为QStyle::SH_DialogButtonLayout styleHint 提供自定义样式。

    class KdeStyle : public QProxyStyle
    {
    public:
        virtual int styleHint(StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const override
        {
            if (stylehint == SH_DialogButtonLayout)
                return QDialogButtonBox::KdeLayout;
    
            return QProxyStyle::styleHint(stylehint, opt, widget, returnData);
        }
    };
    

    然后将自定义样式应用到应用程序。

    qApp->setStyle(new KdeStyle());
    

    另一种方法是使用样式表。 button-layout 属性在QDialogButtonBoxQMessageBox 中指定按钮的布局。可能的值为 0 (WinLayout)、1 (MacLayout)、2 (KdeLayout) 和 3 (GnomeLayout)。

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel);
    buttonBox->setStyleSheet("* { button-layout: 2 }");
    

    【讨论】:

      猜你喜欢
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多