如何使 QPushButton 可按下输入键?

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

问题描述

我想让我的应用程序笔记本电脑友好.我可以选择任何地方,但如果我选择一个 说明了这些:><块引用>

默认和自动默认按钮决定当用户在对话框中按 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

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

上一篇:从 dll 启动 Qt GUI(在 DLLStart 函数中) 下一篇:无框透明窗qt5

相关文章

最新文章