Results 1 to 9 of 9

Thread: MainWindow Button

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  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

    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].

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

    waynew (27th December 2009)

  4. #3
    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 

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

    waynew (27th December 2009)

  6. #4
    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. button background color when it is clicked
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 24th June 2024, 12:09
  2. Replies: 5
    Last Post: 1st March 2010, 15:55
  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
  •  
Qt is a trademark of The Qt Company.