PDA

View Full Version : Form with default button



miraks
25th January 2010, 11:12
Hi,

In the main window (QMainWindow) of my application, I have a form with a QPushButton named "Add".
I would like:
1-"Add" button is a default button.
2-When I click "Enter", the action "Add" is executed.

I tried to use the property autoDefault and default but it doesn't work because my main window (QMainWindow) is not a dialog (QDialog).

How can I do ?

aamer4yu
25th January 2010, 12:18
1-"Add" button is a default button.
use addButton->setFocus(true).


2-When I click "Enter", the action "Add" is executed.

Read about signals and slots

vishwajeet.dusane
25th January 2010, 12:25
Hi
this is difficult to understand.


2-When I click "Enter", the action "Add" is executed.

is "Enter" a new button ? and on click event u are expecting "Add" button should behave as clicked ?

miraks
25th January 2010, 14:35
Hi all,

My button "Add" is already connected with the expected slot.
I just would like that this slot is executed too when the button is enable and when the user hits the key "Enter".

Here is an example of screenshot:
http://skrooge.org/node/4

mattc
25th January 2010, 15:27
QPushButton::setDefault() is the way to go, but the parent widget of the button must be focused. Maybe QWidget::setFocusProxy() can help you handle the focus correctly among your widgets.

miraks
25th January 2010, 15:39
QPushButton::setDefault() is the way to go, but the parent widget of the button must be focused. Maybe QWidget::setFocusProxy() can help you handle the focus correctly among your widgets.

I already tried, but, as documented, this function is working only on QDialog.
It's not my case.

How can I do ?

aamer4yu
25th January 2010, 15:49
A button is not triggered with Enter key. You need to press spacebar.
If you want to trigger the button on Enter key, you will need to install filter on events or override keyPressEvent. You can then call click()

mattc
25th January 2010, 15:50
You are right, it works only for QDialogs.

Some things you might try:

1) as aamer4yu said, use QPushButton::setFocus(true) or QWidget::setFocusProxy() to force focus on the button
2) create a QShortcut, connect to the activated() signal and then call QPushButton->animateClick()
3) override QWidget::keyPressEvent() in the button's parent, detect the Enter key and then call QPushButton->animateClick()