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

问题描述

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

默认和自动默认按钮决定当用户在对话框中按 Enter.

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

在对话框中,一次只能有一个按钮作为默认按钮.该按钮随后会显示一个额外的框架(取决于GUI 风格).

默认按钮行为仅在对话框中提供.按钮可以始终通过按空格键从键盘单击按钮有焦点.

如果当前默认按钮的默认属性设置为false当对话框可见时,新的默认值将自动为下次对话框中的按钮获得焦点时分配.

还值得查看 QDialogQMessageBox.

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 , only with .
What's the problem? How to make it pressable for ?

解决方案

tl;dr

  1. In the Qt Creator's UI view, select the button you want to make pressable for .
  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 .
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 the button with the focus on it will be pressed. The followings will describe what happens if you press .

Default property

If this is set true, the button will be a default button.
If 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 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 .

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 or .

Official documentation

Most of this answer was made according to the official documentation.
The QPushButton documentation made by Qt states these:

It's also worth to check QDialog and QMessageBox.

这篇关于如何使 QPushButton 可按下输入键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:41