問題描述
我想讓我的應用程序筆記本電腦友好.我可以選擇任何地方,但如果我選擇一個 說明了這些:><塊引用>
默認和自動默認按鈕決定當用戶在對話框中按 Enter.
此屬性設置為 true 的按鈕(即對話框的默認值按鈕,) 將在用戶按下 Enter 時自動按下,有一個例外:如果 autoDefault 按鈕當前具有焦點,則按下自動默認按鈕.當對話框有 autoDefault 按鈕時但沒有默認按鈕,按回車將按當前具有焦點的 autoDefault 按鈕,或者如果沒有按鈕焦點,焦點鏈中的下一個自動默認按鈕.
在對話框中,一次只能有一個按鈕作為默認按鈕.該按鈕隨后會顯示一個額外的框架(取決于GUI 風格).
默認按鈕行為僅在對話框中提供.按鈕可以始終通過按空格鍵從鍵盤單擊按鈕有焦點.
如果當前默認按鈕的默認屬性設置為false當對話框可見時,新的默認值將自動為下次對話框中的按鈕獲得焦點時分配.
還值得查看 QDialog 和 QMessageBox.
I want to make my app laptop friendly. I can tab to everywhere, but if I tab to a QPushButton I can't press it with Enter, only with space.
What's the problem? How to make it pressable for Enter?
tl;dr
- 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
.
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.
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.
Every time you press space the button with the focus on it will be pressed. The followings will describe what happens if you press Enter.
Default property
If this is set true, the button will be a default button.
If Enter is pressed on the dialog, than this button will be pressed, except when the focus is on an autoDefault button.
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 Enter is pressed on the dialog, than this button will be pressed if the focus is on it.
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.
Flat property
If this is set true, then the border of the button will not be raised.
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:
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 Enter or space.
Official documentation
Most of this answer was made according to the official documentation.
The QPushButton documentation made by Qt states these:
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.
It's also worth to check QDialog and QMessageBox.
這篇關于如何使 QPushButton 可按下輸入鍵?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!