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?
相关问题
- Sorting 3 numbers without branching [closed]
- QML: Cannot read property 'xxx' of undefin
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
tl;dr
QPushButton
.autoDefault
ordefault
.Most of the cases the main different between
autoDefault
anddefault
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
andautoDefault
) have a major role when we place buttons onQDialog
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 aQDialog
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 atsetDefault()
and/orsetAutoDefault()
.You just need to create a window, add a
QPushButton
calledpushButton
and aQLabel
calledlabel
(that is the default naming). Don't forget to#include <QMessageBox>
. Then copy this code to the button'sclicked()
signal: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:
It's also worth to check QDialog and QMessageBox.
According to Qt's documentation Enter should work
http://qt-project.org/doc/qt-4.8/qpushbutton.html
Set the tab order for your widgets. This will allow usage of return key for clicking. Its in there by default inside Qt.
totymedli's answer is great. I added a small program to test various combinations of isDefault, autoDefault, setDefault and setAutoDefault functions.