ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 如何使QPushButton按下输入键?

2019-09-28 14:07:12  阅读:264  来源: 互联网

标签:qpushbutton c qt


我想让我的应用笔记本电脑友好.我可以选择到任何地方,但是如果我选择到QPushButton我不能用Enter键,只能用空格键.
有什么问题?如何使其可按Enter键?

解决方法:

TL;博士

>在Qt Creator的UI视图中,选择要按Enter键的按钮.
>在属性编辑器的右侧,向下滚动到标题为QPushButton的蓝色部分.
>选中autoDefault复选框或默认值.

大多数情况下autoDefault和default之间的主要区别在于如何呈现按钮.但是有些情况下它会导致意想不到的事情,所以更多信息请参阅下文.

全面审查

概观

每个QPushButton都有3个不被继承的属性.从这些开始,当我们在QDialogs上放置按钮时,两个(默认和autoDefault)起主要作用,因为这些设置(以及其中一个按钮的焦点)决定了当我们按Enter键时将按下哪个按钮.
默认情况下,所有这些属性都设置为false.只有expction是autoDefault,如果按钮具有QDialog父级,则为true.

每次按空格键时,都会按下对焦于该按钮的按钮.以下将描述如果按Enter键会发生什么.

默认属性

如果设置为true,则该按钮将是默认按钮.
如果在对话框上按下Enter,则按下此按钮,除非焦点位于autoDefault按钮上.

应该只有一个默认按钮.如果添加更多,则添加的最后一个将是默认按钮.

AutoDefault属性

如果设置为true,则该按钮将是autoDefault按钮.
如果在对话框上按下Enter键,则在焦点位于此按钮时将按下此按钮.

如果焦点不在autoDefault按钮上,并且没有默认按钮,则按Enter键将触发下一个autoDefault按钮.

扁平的财产

如果设置为true,则不会引发按钮的边框.

示例表

下表显示了在不同焦点上使用不同按钮按下哪个按钮.按钮从左到右添加.

测试代码

以下代码是一种向对话框添加按钮的方法.它可以通过更改setDefault()和/或setAutoDefault()的布尔值来用于测试.
您只需要创建一个窗口,添加一个名为pushButton的QPushButton和一个名为QLabel的标签(这是默认命名).不要忘记#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");
   }
}

显示

如果您编译代码,您可以获得此窗口.您甚至不必单击按钮,因为操作系统呈现它们的方式显示如果您按Enter键或空格键将按下哪个按钮.

官方文件

大部分答案都是根据官方文件做出的.
Qt制造的QPushButton documentation表示:

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.

检查QDialogQMessageBox也是值得的.

标签:qpushbutton,c,qt
来源: https://codeday.me/bug/20190928/1827748.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有