如何更改 QDialogBu​​ttonBox 中按钮的标题? [英] How to change the caption of a button in a QDialogButtonBox?

查看:53
本文介绍了如何更改 QDialogBu​​ttonBox 中按钮的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个带有默认 CancelOK 按钮的 QDialogBu​​ttonBox 按钮.

I have added a QDialogButtonBox button with the default Cancel and OK buttons.

有没有办法更改这些按钮的标题?例如,OK 应该变成 Run.

Is there a way to change the caption of these buttons? For example, OK should become Run.

推荐答案

您必须在 cpp 文件中进行一些轻微的编码:

You will have to do some slight coding in your cpp file:

ui->buttonBox->button(QDialogButtonBox::Ok)->setText("Run");
ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("Exit");

请注意,您可能还需要包含 QPushButton 标头:

Note that you may also need to include the QPushButton header:

#include <QPushButton>

更新:

没有注意到 pyqt 标签.我不熟悉 Python(尤其是 PyQt),但我认为这应该可以:

Update:

Did not notice the pyqt tag. I'm not familiar with Python (and PyQt in particular), but I think this should do the job:

self.ui.buttonBox.button(QDialogButtonBox.Ok).setText("Run")
self.ui.buttonBox.button(QDialogButtonBox.Cancel).setText("Cancel")

此外,正如 Kuba Ober 所指出的,更改标准按钮的文本并不是最好的方法.最正确的方法是添加具有适当角色的自定义按钮.

Also, as pointed out by Kuba Ober, changing the text of standard buttons is not the best approach. The most correct way is to add custom buttons with an appropriate role.

self.ui.buttonBox.addButton("Run", QDialogButtonBox.ActionRole)

这篇关于如何更改 QDialogBu​​ttonBox 中按钮的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆