C++ 如何使 QPushButton 可按下输入键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11887938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a QPushButton pressable for enter key?
提问by totymedli
I want to make my app laptop friendly. I can tab to everywhere, but if I tab to a QPushButtonI can't press it with Enter, only with space.
What's the problem? How to make it pressable for Enter?
我想让我的应用程序笔记本电脑友好。我可以随意切换到任何地方,但是如果我切换到QPushButton,我不能用 按下它Enter,只能用space.
有什么问题?如何使它可按Enter?
回答by totymedli
tl;dr
tl;博士
- In the Qt Creator's UI view, select the button you want to make pressable for Enter.
- In the right side at the Property Editor scroll down to the blue part titled
QPushButton
. - Check the checkbox by
autoDefault
ordefault
.
- 在 Qt Creator 的 UI 视图中,选择要使其可按下的按钮Enter。
- 在右侧的 Property Editor 中向下滚动到标题为 的蓝色部分
QPushButton
。 - 通过
autoDefault
或选中复选框default
。
Most of the cases the main different between autoDefault
and default
is how the button will be rendered. But there are cases where it can cause unexpected things so for more information read more below.
大多数病例之间的主要不同autoDefault
,并default
是该按钮将如何呈现。但在某些情况下,它可能会导致意想不到的事情,因此有关更多信息,请阅读下面的更多内容。
Full review
全面
Overview
概述
Every QPushButton
has 3 properties that are not inherited. From these, two (default
and autoDefault
) have a major role when we place buttons on QDialog
s, because these settings (and the focus on one of the buttons) decides which button will be pressed if we hit Enter.
All of these properties are set false by default. Only expection is autoDefault
that will be true if the button has a QDialog
parent.
每个QPushButton
都有 3 个未继承的属性。其中,当我们在s上放置按钮时,两个(default
和autoDefault
)起着主要作用QDialog
,因为这些设置(以及对其中一个按钮的关注)决定了如果我们点击 将按下哪个按钮Enter。
默认情况下,所有这些属性都设置为 false。唯一的期望是autoDefault
如果按钮有QDialog
父级,那将是真的。
Every time you press spacethe button with the focus on it will be pressed. The followings will describe what happens if you press Enter.
每次您按下space带有焦点的按钮时,它就会被按下。下面将描述如果您按下 会发生什么Enter。
Default property
默认属性
If this is set true, the button will be a default button.
If Enteris pressed on the dialog, than this button will be pressed, except when the focus is on an autoDefault button.
如果设置为 true,则按钮将是默认按钮。
如果Enter在对话框上按下 ,则将按下此按钮,除非焦点位于 autoDefault 按钮上。
There should be only one default button. If you add more then the last one added will be the default button.
应该只有一个默认按钮。如果您添加更多,则添加的最后一个将是默认按钮。
AutoDefault property
自动默认属性
If this is set true, the button will be an autoDefault button.
If Enteris pressed on the dialog, than this button will be pressed if the focus is on it.
如果设置为 true,则按钮将是autoDefault button。
如果Enter在对话框上按下 ,则如果焦点在其上,则将按下此按钮。
If the focus is not on an autoDefault button and there is no default button than the next autoDefault button will be pressed for Enter.
如果焦点不在 autoDefault 按钮上并且没有默认按钮,则将按下下一个 autoDefault 按钮Enter。
Flat property
平房
If this is set true, then the border of the button will not be raised.
如果设置为 true,则按钮的边框不会升高。
Example tables
示例表
The following tables show which button will be pressed with different buttons on different focus. The buttons are added from left to right.
下表显示了不同焦点上的不同按钮将按下哪个按钮。按钮是从左到右添加的。
Test code
测试代码
The following code is a way to add buttons to a dialog. It can be used for testing by changing the boolean
values at setDefault()
and/or setAutoDefault()
.
You just need to create a window, add a QPushButton
called pushButton
and a QLabel
called label
(that is the default naming). Don't forget to #include <QMessageBox>
. Then copy this code to the button's clicked()
signal:
以下代码是一种向对话框添加按钮的方法。它可用于通过更改和/或boolean
处的值进行测试。
您只需要创建一个窗口,添加一个被调用和一个被调用(这是默认命名)。不要忘记。然后将此代码复制到按钮的信号中:setDefault()
setAutoDefault()
QPushButton
pushButton
QLabel
label
#include <QMessageBox>
clicked()
void MainWindow::on_pushButton_clicked()
{
QMessageBox msgBox;
QPushButton button("Button");
button.setDefault(false);
button.setAutoDefault(false);
msgBox.addButton(&button, QMessageBox::ActionRole);
QPushButton autodefaultbutton("AutoDefault Button");
autodefaultbutton.setDefault(false);
autodefaultbutton.setAutoDefault(true);
msgBox.addButton(&autodefaultbutton, QMessageBox::ActionRole);
QPushButton autodefaultbutton2("AutoDefault Button2");
autodefaultbutton2.setDefault(false);
autodefaultbutton2.setAutoDefault(true);
msgBox.addButton(&autodefaultbutton2, QMessageBox::ActionRole);
QPushButton defaultbutton("Default Button");
defaultbutton.setDefault(true);
defaultbutton.setAutoDefault(false);
msgBox.addButton(&defaultbutton, QMessageBox::ActionRole);
msgBox.exec();
if (msgBox.clickedButton() == &button) {
ui->label->setText("Button");
} else if (msgBox.clickedButton() == &defaultbutton) {
ui->label->setText("Default Button");
} else if (msgBox.clickedButton() == &autodefaultbutton) {
ui->label->setText("AutoDefault Button");
} else if (msgBox.clickedButton() == &autodefaultbutton2) {
ui->label->setText("AutoDefault Button2");
}
}
Display
展示
If you compile the code you can get this window. You doesn't even have to click to the buttons because the way they are rendered by the OS shows which one will be pressed if you hit Enteror space.
如果你编译代码,你可以得到这个窗口。您甚至不必单击按钮,因为操作系统呈现它们的方式会显示如果您点击Enter或将按下哪个按钮space。
Official documentation
官方文档
Most of this answer was made according to the official documentation.
The QPushButton documentationmade by Qt states these:
这个答案大部分是根据官方文档做出的。Qt 制作
的QPushButton 文档说明了这些:
Default and autodefault buttons decide what happens when the user presses enter in a dialog.
A button with this property set to true (i.e., the dialog's default button,) will automatically be pressed when the user presses enter, with one exception: if an autoDefault button currently has focus, the autoDefault button is pressed. When the dialog has autoDefault buttons but no default button, pressing enter will press either the autoDefault button that currently has focus, or if no button has focus, the next autoDefault button in the focus chain.
In a dialog, only one push button at a time can be the default button. This button is then displayed with an additional frame (depending on the GUI style).
The default button behavior is provided only in dialogs. Buttons can always be clicked from the keyboard by pressing Spacebar when the button has focus.
If the default property is set to false on the current default button while the dialog is visible, a new default will automatically be assigned the next time a pushbutton in the dialog receives focus.
默认和自动默认按钮决定当用户在对话框中按下 Enter 键时会发生什么。
此属性设置为 true 的按钮(即对话框的默认按钮)将在用户按下 Enter 键时自动按下,但有一个例外:如果 autoDefault 按钮当前具有焦点,则按下 autoDefault 按钮。当对话框有 autoDefault 按钮但没有默认按钮时,按 Enter 键将按下当前具有焦点的 autoDefault 按钮,或者如果没有按钮具有焦点,则按下焦点链中的下一个 autoDefault 按钮。
在对话框中,一次只有一个按钮可以是默认按钮。此按钮随后会显示一个附加框架(取决于 GUI 样式)。
默认按钮行为仅在对话框中提供。当按钮具有焦点时,始终可以通过按空格键从键盘单击按钮。
如果在对话框可见时将当前默认按钮的默认属性设置为 false,则下次对话框中的按钮获得焦点时将自动分配一个新的默认值。
It's also worth to check QDialogand QMessageBox.
也值得检查QDialog和QMessageBox。
回答by Amxx
According to Qt's documentation Enter should work
根据 Qt 的文档 Enter 应该可以工作
Command buttons in dialogs are by default auto-default buttons, i.e. they become the default push button automatically when they receive the keyboard input focus. A default button is a push button that is activated when the user presses the Enter or Return key in a dialog. You can change this with setAutoDefault().
对话框中的命令按钮在默认情况下是自动默认按钮,即当它们接收到键盘输入焦点时自动成为默认按钮。默认按钮是当用户在对话框中按下 Enter 或 Return 键时激活的按钮。您可以使用 setAutoDefault() 更改此设置。
回答by nenad
totymedli's answer is great. I added a small program to test various combinations of isDefault, autoDefault, setDefault and setAutoDefault functions.
totymedli 的回答很棒。我添加了一个小程序来测试 isDefault、autoDefault、setDefault 和 setAutoDefault 函数的各种组合。
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
autoDefaultInitialState = True
defaultInitialState = False
self.lineEdit1 = QLineEdit(self)
self.lineEdit2 = QLineEdit(self)
self.lineEdit3 = QLineEdit(self)
# if we create a new button (e.g. "Print state"), with the same function,
# it doesn't work, because adding a new button (apart from our 3 buttons)
# produces total mess, so we use this lineedit for this purpose
self.lineEdit1.returnPressed.connect(self.printState)
#------------------------------------
self.chkAutoDefaultOk = QCheckBox('OK setAutoDefault', self)
self.chkAutoDefaultCancel = QCheckBox('Cancel setAutoDefault', self)
self.chkAutoDefaultApply = QCheckBox('Apply setAutoDefault', self)
self.chkDefaultOk = QCheckBox('OK setDefault', self)
self.chkDefaultCancel = QCheckBox('Cancel setDefault', self)
self.chkDefaultApply = QCheckBox('Apply setDefault', self)
self.chkAutoDefaultOk.setChecked(autoDefaultInitialState)
self.chkAutoDefaultCancel.setChecked(autoDefaultInitialState)
self.chkAutoDefaultApply.setChecked(autoDefaultInitialState)
self.chkDefaultOk.setChecked(defaultInitialState)
self.chkDefaultCancel.setChecked(defaultInitialState)
self.chkDefaultApply.setChecked(defaultInitialState)
#------------------------------------
self.pushButtonOk = QPushButton(self)
self.pushButtonOk.setText("Ok")
self.pushButtonOk.clicked.connect(lambda : print('ok'))
self.pushButtonCancel = QPushButton(self)
self.pushButtonCancel.setText("Cancel")
self.pushButtonCancel.clicked.connect(lambda : print('cancel'))
self.pushButtonApply = QPushButton(self)
self.pushButtonApply.setText("Apply")
self.pushButtonApply.clicked.connect(lambda : print('apply'))
#------------------------------------
self.pushButtonOk.setAutoDefault(autoDefaultInitialState)
self.pushButtonCancel.setAutoDefault(autoDefaultInitialState)
self.pushButtonApply.setAutoDefault(autoDefaultInitialState)
self.pushButtonOk.setDefault(defaultInitialState)
self.pushButtonCancel.setDefault(defaultInitialState)
self.pushButtonApply.setDefault(defaultInitialState)
#------------------------------------
self.chkAutoDefaultOk.stateChanged.connect(self.chkChangeState)
self.chkAutoDefaultCancel.stateChanged.connect(self.chkChangeState)
self.chkAutoDefaultApply.stateChanged.connect(self.chkChangeState)
self.chkDefaultOk.stateChanged.connect(self.chkChangeState)
self.chkDefaultCancel.stateChanged.connect(self.chkChangeState)
self.chkDefaultApply.stateChanged.connect(self.chkChangeState)
#------------------------------------
self.layout = QGridLayout(self)
self.layout.addWidget(self.lineEdit1, 0, 0, 1, 3)
self.layout.addWidget(self.lineEdit2, 1, 0, 1, 3)
self.layout.addWidget(self.lineEdit3, 2, 0, 1, 3)
self.layout.addWidget(self.chkAutoDefaultOk, 3, 0)
self.layout.addWidget(self.chkAutoDefaultCancel, 3, 1)
self.layout.addWidget(self.chkAutoDefaultApply, 3, 2)
self.layout.addWidget(self.chkDefaultOk, 4, 0)
self.layout.addWidget(self.chkDefaultCancel, 4, 1)
self.layout.addWidget(self.chkDefaultApply, 4, 2)
self.layout.addWidget(self.pushButtonOk, 5, 0)
self.layout.addWidget(self.pushButtonCancel, 5, 1)
self.layout.addWidget(self.pushButtonApply, 5, 2)
def chkChangeState(self):
obj = self.sender()
if (obj == self.chkAutoDefaultOk):
self.pushButtonOk.setAutoDefault(self.chkAutoDefaultOk.isChecked())
elif (obj == self.chkAutoDefaultCancel):
self.pushButtonCancel.setAutoDefault(self.chkAutoDefaultCancel.isChecked())
elif (obj == self.chkAutoDefaultApply):
self.pushButtonApply.setAutoDefault(self.chkAutoDefaultApply.isChecked())
elif (obj == self.chkDefaultOk):
self.pushButtonOk.setDefault(self.chkDefaultOk.isChecked())
elif (obj == self.chkDefaultCancel):
self.pushButtonCancel.setDefault(self.chkDefaultCancel.isChecked())
elif (obj == self.chkDefaultApply):
#print('sender: self.chkDefaultApply')
#print('before: ', self.pushButtonApply.isDefault())
self.pushButtonApply.setDefault(self.chkDefaultApply.isChecked())
#print('after: ', self.pushButtonApply.isDefault())
def printState(self):
print(self.pushButtonOk.autoDefault(), self.pushButtonCancel.autoDefault(), self.pushButtonApply.autoDefault())
print(self.pushButtonOk.isDefault(), self.pushButtonCancel.isDefault(), self.pushButtonApply.isDefault())
print('-' * 50)
app = QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
回答by G Sree Teja Simha
Set the tab order for your widgets. This will allow usage of return key for clicking. Its in there by default inside Qt.
设置小部件的 Tab 键顺序。这将允许使用返回键进行点击。默认情况下,它在 Qt 中。