Results 1 to 9 of 9

Thread: MainWindow Button

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default MainWindow Button

    I have a pushbutton on the main window that is set to be the default.
    Now, how can I make it click with the enter key?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MainWindow Button

    you can install event filter on you button or subclass QPushButton and reimplement keyPressEvent.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    waynew (26th December 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MainWindow Button

    Have you connected its 'clicked()' signal to anything?

    Assuming you used the designer to create the mainwindow, right click on the button and go to its clicked slot and implement what you want the button to do here. Being the default button, it should automatically receive focus when the main window opens.

    the following code will pop up a Hello World messagebox.

    Qt Code:
    1. void MainWindow::xxxButton_clicked()
    2. {
    3. QMessageBox::critical(0, "HELLO MSG","Hello World");
    4. }
    To copy to clipboard, switch view to plain text mode 

    I hope this helps.

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MainWindow Button

    Quote Originally Posted by JD2000 View Post
    Have you connected its 'clicked()' signal to anything?

    Assuming you used the designer to create the mainwindow, right click on the button and go to its clicked slot and implement what you want the button to do here. Being the default button, it should automatically receive focus when the main window opens.

    the following code will pop up a Hello World messagebox.

    Qt Code:
    1. void MainWindow::xxxButton_clicked()
    2. {
    3. QMessageBox::critical(0, "HELLO MSG","Hello World");
    4. }
    To copy to clipboard, switch view to plain text mode 

    I hope this helps.
    author wants handle enter key press, how does it connect with clicked?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MainWindow Button

    I may be misunderstanding the documentation, but the following has been cut and pasted directly from the QPushButton class reference notes:

    "A push button emits the signal clicked() when it is activated by the mouse, the Spacebar or by a keyboard shortcut. Connect to this signal to perform the button's action. Push buttons also provide less commonly used signals, for example, pressed() and released().

    Command buttons in dialogs are by default auto-default buttons, i.e. they become the default push button automatically when they receive the keyboard input focus. A default button is a push button that is activated when the user presses the Enter or Return key in a dialog. You can change this with setAutoDefault(). Note that auto-default buttons reserve a little extra space which is necessary to draw a default-button indicator. If you do not want this space around your buttons, call setAutoDefault(false).

    Being so central, the button widget has grown to accommodate a great many variations in the past decade. The Microsoft style guide now shows about ten different states of Windows push buttons and the text implies that there are dozens more when all the combinations of features are taken into consideration.

    The most important modes or states are:

    * Available or not (grayed out, disabled).
    * Standard push button, toggling push button or menu button.
    * On or off (only for toggling push buttons).
    * Default or normal. The default button in a dialog can generally be "clicked" using the Enter or Return key.
    * Auto-repeat or not.
    * Pressed down or not."

    This seems to indicate that my advice is correct.

  7. #6
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MainWindow Button

    Sorry JD2000, I probably didn't state the problem clearly.

    The pushbutton works fine. I just want it clicked when the Enter key is hit.
    This is automatic on the default button in a dialog, but not a mainwindow.

    Here is what I have tried but it does nothing:

    Qt Code:
    1. ui->pbSave->installEventFilter(this);
    2.  
    3. bool MainWindow::eventFilter(QObject *object, QEvent *event)
    4. {
    5. if (object == ui->pbSave && event->type() == QEvent::KeyPress) {
    6. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    7. if (keyEvent->key() == Qt::Key_Enter) {
    8. ui->pbSave->click();
    9. return true;
    10. } else
    11. return false;
    12. }
    13. return false;
    14. }
    To copy to clipboard, switch view to plain text mode 

    I am probably not understanding the event filter doc correctly.
    Any help appreciated.

  8. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: MainWindow Button

    here is work example
    Qt Code:
    1. Test::Test(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QVBoxLayout *vbl = new QVBoxLayout(this);
    5. m_pbTest = new QPushButton(tr("test"));
    6. m_pbTest->installEventFilter(this);
    7. vbl->addWidget(m_pbTest);
    8. connect(m_pbTest, SIGNAL(clicked()), SLOT(testClicked()));
    9. }
    10.  
    11. bool Test::eventFilter(QObject *o, QEvent *e)
    12. {
    13. if (o == m_pbTest && e->type() == QEvent::KeyPress) {
    14. QKeyEvent *ke = static_cast<QKeyEvent *>(e);
    15. if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
    16. m_pbTest->click();
    17. }
    18. return QWidget::eventFilter(o, e);
    19. }
    20.  
    21. void Test::testClicked()
    22. {
    23. qDebug() << Q_FUNC_INFO << "clicked";
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    waynew (27th December 2009)

  10. #8
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: MainWindow Button

    If you want the button clicked on enter when it has the focus, you can just call setAutoDefault with true. If you want it clicked when pressing enter while it doesn't have the focus, you will have to intercept events for the QMainWindow rather than the button. You can do this by overriding the event function for QMainWindow.
    Qt Code:
    1. bool MainWindow::event(QEvent *event)
    2. {
    3. if (event->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent *ke = static_cast<QKeyEvent *>(event);
    6. if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
    7. {
    8. qDebug() << "click";
    9. ui->pbSave->click();
    10. return true;
    11. }
    12. }
    13. return QMainWindow::event(event);
    14. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to numbat for this useful post:

    waynew (27th December 2009)

  12. #9
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: MainWindow Button

    Thanks folks. Now I understand the possible approaches to the problem much better.

    Working fine now.

    Thanks to all of the experienced ones who are willing to help us learners!

Similar Threads

  1. Replies: 5
    Last Post: 1st March 2010, 15:55
  2. button background color when it is clicked
    By navi1084 in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 15:02
  3. button with backgr and icon using stylesheets
    By s_p_t10 in forum Qt Programming
    Replies: 0
    Last Post: 7th May 2008, 20:19
  4. Button on the Upper Right Corner of MainWindow?
    By vishal.chauhan in forum Qt Programming
    Replies: 10
    Last Post: 12th March 2008, 10:47
  5. Replies: 1
    Last Post: 11th September 2007, 13:34

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.