QT开发中改变 QMessageBox等对话框按钮的左右顺序 您所在的位置:网站首页 如何调整按键显示的位置 QT开发中改变 QMessageBox等对话框按钮的左右顺序

QT开发中改变 QMessageBox等对话框按钮的左右顺序

2024-06-24 04:56| 来源: 网络整理| 查看: 265

在Qt的界面开发中,QMessageBox,QDialogButtonBox等对话框的按钮位置或顺序,跟所在系统有关,如果是Linux系统,会显示成下面的布局: Linux系统下的安装布局 这和在Windows系统下的习惯不同。 如果想让 OK 按钮在左边,Cancel 在右边,可以按如下操作, 设计一个新的 QProxyStyle类,如WinStyle,继承自QProxyStyle,并重载它的styleHint 方法,当 hint 是SH_DialogButtonLayout ,返回自己想要的布局 QDialogButtonBox::WinLayout ,代码如下:

#include #include #include #include #include "mainwindow.h" class WinStyle : public QProxyStyle { public: int styleHint(StyleHint hint, const QStyleOption *option = 0, const QWidget *widget = 0, QStyleHintReturn *returnData = 0) const override { if ( hint == SH_DialogButtonLayout ) { return QDialogButtonBox::WinLayout ; } return QProxyStyle::styleHint(hint, option, widget, returnData); } };

然后把这个自己定制的style给 QApplication设置上,用 setStyle( new WinStyle ) ,代码如下:

int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setStyle( new WinStyle ) ; MainWindow w; w.show(); return a.exec(); }

然后效果就是这样了,比较习惯一些:. Windows下习惯的按钮布局。在这里插入图片描述 也可以用style sheet实现:

QMessageBox msgbox( this ) ; msgbox.setStyleSheet("* { button-layout: 0 }");

其中 0 表示WinLayout ,其它可能的值有 1 (MacLayout), 2 (KdeLayout), 3 (GnomeLayout).

联系方式 QQ : 83555727 Email [email protected]



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有