Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position of the "Apply" button in QDialogButtonBox

Tags:

qt

I would like to have a QDialogButtonBox with three buttons in this particular order:

Ok | Apply | Cancel

Is it possible to reorder the buttons to put Apply in the center?

like image 214
tung Avatar asked Mar 10 '16 02:03

tung


1 Answers

The button layout is platform specific.

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

There is two way to force use non standard layout.

You can subclass QProxyStyle and reimplement styleHint method, to provide custom style for 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);
    }
};

Then apply custom style to application.

qApp->setStyle(new KdeStyle());

Another way to do it, is using stylesheets. button-layout property specify the layout of buttons in a QDialogButtonBox or a QMessageBox. The possible values are 0 (WinLayout), 1 (MacLayout), 2 (KdeLayout), and 3 (GnomeLayout).

QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel);
buttonBox->setStyleSheet("* { button-layout: 2 }");
like image 125
Meefte Avatar answered Nov 05 '22 18:11

Meefte