Talespin_Kit
Talespin_Kit

Reputation: 21897

How to swap the "Cancel" and "Ok" button on QInputDialog widget?

The following code displays input dialog with the "cancel" button on the left and the "Ok" button on the right. How to swap the position of the buttons ?

QInputDialog::getText(this, QString("Email"), QString("Enter email address:"), QLineEdit::Normal, QString(""), &ok, 0);

Upvotes: 1

Views: 1163

Answers (1)

Frank Osterfeld
Frank Osterfeld

Reputation: 25165

When it comes to the button order, Qt follows the convention of the environment it is running in, i.e. Ok/Cancel on Windows, KDE, etc., and Cancel/OK on OS X, Gnome etc. That's generally a good thing, one shouldn't surprise the user with a button order other than the platform default.

However, if you have a good reason to override the platform default (I can't think of any) or if you are e.g. on an embedded platform where there is no platform default, you need to override what the used QStyle defines.

Implement your own proxy style, overriding the value returned for QStyle::SH_DialogButtonLayout:

int ProxyStyle::styleHint(StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0) const override {
   if (hint == SH_DialogButtonLayout) {
       return QDialogButtonBox::WinLayout;
   }
   return baseStyle()->styleHint(hint, option, widget, returnData);
}

Upvotes: 5

Related Questions