QMessageBox自定义按钮文本中文显示,按钮个数等

QMessageBox自定义按钮文本中文显示,按钮个数等_qmessagebox按钮文字-CSDN博客

一般用的是参数最多那个重载。

    static int information(QWidget *parent, const QString &title,
                           const QString& text,
                           const QString& button0Text,
                           const QString& button1Text = QString(),
                           const QString& button2Text = QString(),
                           int defaultButtonNumber = 0,
                           int escapeButtonNumber = -1);
    static int question(QWidget *parent, const QString &title,
                        const QString& text,
                        const QString& button0Text,
                        const QString& button1Text = QString(),
                        const QString& button2Text = QString(),
                        int defaultButtonNumber = 0,
                        int escapeButtonNumber = -1);
    static int warning(QWidget *parent, const QString &title,
                       const QString& text,
                       const QString& button0Text,
                       const QString& button1Text = QString(),
                       const QString& button2Text = QString(),
                       int defaultButtonNumber = 0,
                       int escapeButtonNumber = -1);

很明显这三种用法都一样。

parent:指定对话框的父级对象。如果不指定,对话框是相对于整个application的。如果指定了,比如我为窗体指定了qss,它会顺延到对话框。

titile和text就不用说了。

button*Text可以分别指定按钮文本,指定几个就显示几个按钮。不用管它怎么和那些枚举对应上,用惯了枚举思维就禁锢了,其实没必要。随便根据需要指定就好。

defaultButtonNumber指定默认按钮的索引号,也就是直接按回车是哪个按钮。刚才那三个按钮编号是0,1,2。

escapeButtonNumber指定按esc执行的那个按钮的索引号。

下面直接按照三个按钮的方式举例,如果是两个或者一个按钮,酌情调整即可。

    int iBtn = QMessageBox::information(this, "标题", "文本", "是", "否", "取消", 0, 2);
    switch (iBtn) {
    case 0:
        QMessageBox::information(this, "", "是");
        break;
    case 1:
        QMessageBox::information(this, "", "否");
        break;
    case 2:
        QMessageBox::information(this, "", "取消");
        break;
    default:
        break;
    }

假设要显示两个或者一个按钮,那就把buttonText只设置一个就行了,后面对应的defaultButtonNumber和escapeButtonNumber记者对应好,别指定无效值就行。

如此以来,对话框可以随便定制按钮,甚至比qt预定义的枚举更灵活。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值