Results 1 to 8 of 8

Thread: Form with default button

  1. #1
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Form with default button

    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 ?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Form with default button

    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

  3. #3
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Form with default button

    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 ?

  4. #4
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Form with default button

    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

  5. #5
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Form with default button

    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.
    Last edited by mattc; 25th January 2010 at 15:32.

  6. #6
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Form with default button

    Quote Originally Posted by mattc View Post
    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 ?

  7. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Form with default button

    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()

  8. #8
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Form with default button

    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()

Similar Threads

  1. Replies: 1
    Last Post: 29th July 2009, 20:25
  2. Replies: 10
    Last Post: 6th July 2008, 09:46
  3. QDialog and default button.
    By hicpalm in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2008, 11:11
  4. Message on close(x) button of Form
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 09:39
  5. How to create an Insert/Submit button for a form.
    By fnmblot in forum Qt Programming
    Replies: 5
    Last Post: 4th August 2006, 16:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.