如何使QPushButton可按下Enter键? [英] How to make a QPushButton pressable for enter key?

查看:145
本文介绍了如何使QPushButton可按下Enter键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使我的应用笔记本电脑更友好。我可以选择到任何地方,但是如果我选择指出:


默认和自动默认按钮决定当用户
在对话框中按Enter键时会发生什么。



带有当用户按下Enter键时,此属性设置为true(即对话框的默认
按钮)将自动被按下,
例外:如果autoDefault按钮当前具有焦点,则
autoDefault按下按钮。当对话框中有autoDefault按钮
但没有默认按钮时,按Enter将按当前具有焦点的
autoDefault按钮,或者如果没有按钮具有
焦点,则焦点中的下一个autoDefault按钮



在对话框中,一次只能有一个按钮是默认按钮。
然后,该按钮会显示一个额外的框架(取决于GUI样式
)。



默认按钮行为仅在对话框中提供。当
按钮具有焦点时,可以始终通过按下空格键从键盘上单击



如果默认属性在当前默认按钮
(当对话框可见时),下次对话框中的按钮下次获得焦点时,将自动为新的默认按钮
分配。


还值得检查 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

  1. In the Qt Creator's UI view, select the button you want to make pressable for Enter.
  2. In the right side at the Property Editor scroll down to the blue part titled QPushButton.
  3. Check the checkbox by autoDefault or 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.


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 QDialogs, 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可按下Enter键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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