【发布时间】:2011-02-09 21:49:15
【问题描述】:
我一直在网上搜索无济于事。有谁知道如何访问按钮框中的按钮(使用“Dialog with Buttons Right”模板创建)?
【问题讨论】:
我一直在网上搜索无济于事。有谁知道如何访问按钮框中的按钮(使用“Dialog with Buttons Right”模板创建)?
【问题讨论】:
在 Designer 中,选择 OK 或 Cancel 按钮。然后打开属性编辑器并向下滚动到QDialogButtonBox 部分。然后,您可以展开 standardButtons 项目以查看可用的各种按钮。也可以使用其他属性,例如 centerButtons 属性。
但是,设计器几乎不能让您控制按钮框。
在代码中,您可以执行许多其他操作,例如更改“标准按钮”上显示的文本。来自documentation:
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true);
moreButton->setAutoDefault(false);
buttonBox = new QDialogButtonBox(Qt::Vertical);
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);
只要在设计器中给按钮框起个名字,就可以在代码中设置这些属性。
【讨论】:
我正在为 Python 社区写这个答案。我正在使用 PySide 并遇到了类似的问题。我有一个 QDialogButtonBox,我想拥有自己的按钮而不是默认按钮。
我正在使用 PySide,它或多或少是 c++ 代码的精确复制品,所以我相信其他 c++ 开发人员也可以从中得到一些东西。
我会怎么做:
my_ok_button = QtGui.QPushButton("My Ok Button")
my_cancel_button = QtGui.QPushButton("My Cancel Button")
ok_cancel_button = QtGui.QDialogButtonBox(QtCore.Qt.Horizontal)
ok_cancel_button.addButton(my_ok_button, QtGui.QDialogButtonBox.ButtonRole.AcceptRole)
ok_cancel_button.addButton(my_cancel_button, QtGui.QDialogButtonBox.ButtonRole.RejectRole)
然后我会像往常一样将我的按钮框插入到我的布局中:
layout.addWidget(ok_cancel_button, 1, 1)
现在在我的代码中,我可以用我的按钮做任何事情。让我们改变它的名字:
my_ok_button.setText("Some Other Name")
那么这里需要注意的是:
你必须在 addButton() 方法中设置按钮的角色,如果你
想要使用标准按钮提供的功能。例如。如果你
希望做类似下面的事情,你需要有按钮角色
设置。
ok_cancel_button.accepted.connect(self.ok_method_handler) ok_cancel_button.rejected.connect(self.close)
【讨论】: