Results 1 to 3 of 3

Thread: Return key in an editable QComboBox accepts a QDialog

  1. #1
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Return key in an editable QComboBox accepts a QDialog

    I have an editable QComboBox inside a QDialog. When I type some text in the QComboBox and I press return, I would like my dialog to do some things. So I've tried to connect the returnPressed() signal from QComboBox::lineEdit() to my slot, but when I press return, the dialog is accepted. That should be the normal behaviour when the focus is in other widgets and the user presses return, but not in this widget. Does someone know how to prevent the QDialog from being closed in this case?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Return key in an editable QComboBox accepts a QDialog

    QComboBox and the QLineEdit inside both ignore key press events by default. This means that these events will be propagated to the parent widget, the dialog in your case.

    One workaround is to subclass QComboBox and override keyPressEvent() to change the behaviour. You should get the returnPressed() signal in the normal way. The only difference is that the event won't be delivered to the dialog. The code could look more or less like this:
    Qt Code:
    1. void MyComboBox::keyPressEvent(QKeyEvent* e)
    2. {
    3. // let base class handle the event
    4. QComboBox::keyPressEvent(e);
    5.  
    6. if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)
    7. {
    8. // accept enter/return events so they won't
    9. // be ever propagated to the parent dialog..
    10. e->accept();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following 4 users say thank you to jpn for this useful post:

    eelik (12th February 2008), fAX (19th March 2012), Koas (29th May 2009), vfernandez (3rd June 2006)

  4. #3
    Join Date
    Jul 2008
    Location
    East Coast, USA
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Return key in an editable QComboBox accepts a QDialog

    Just to say Thanks.

    Couldn't get the thanks button to work

Similar Threads

  1. How to return something not int with a QDialog
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2006, 17:05

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.