Results 1 to 7 of 7

Thread: Emulating Enter key with left mouse click

  1. #1
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Emulating Enter key with left mouse click

    I have an editable QComboBox widget in my application. To change the text at the currentIndex, a user would select the QComboBox, type some text, and then press Enter/Return on their keyboard to solidify the changes.

    However, many users erroneously assume that (left) clicking off of the QComboBox (to some other widget for example) after entering their text is equivalent to pressing Enter/Return when, in fact, it is not. In this case, the user's text changes have not been set "permanently".

    Q1: Is there a way that I programmatically emulate the pressing of an Enter/Return key for the QLineWidget if a user has left-clicked off of an editable QComboBox widget?

    I have the beginnings of what I am trying to do and could use your help:

    Qt Code:
    1. void myClass::mousePressEvent(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::LeftButton) {
    4. // DO SOMETHING HERE: perhaps something like myComboBox->lineEdit()->....
    5. // ...
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Emulating Enter key with left mouse click

    It's better to reimplement focusOutEvent() for the widget's lineEdit() (either as a subclass or using event filters). You can emit returnPressed() there or send a key event to fake return.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Emulating Enter key with left mouse click

    Qt Code:
    1. You can emit returnPressed() there or send a key event to fake return.
    To copy to clipboard, switch view to plain text mode 

    Can you supply an example of either of these? I'm not sure how to do the above. Thanks!

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Emulating Enter key with left mouse click

    You mean something like:

    Qt Code:
    1. void myClass::focusOutEvent()
    2. {
    3. returnPressed();
    4. baseClass:focusOutEvent(); // call base class implementation
    5. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Emulating Enter key with left mouse click

    Going with wysota's original suggestion, I've made headway:

    Qt Code:
    1. bool MyClass::eventFilter(QObject *o, QEvent *e)
    2. {
    3. if ((o == ui->dnNameCombo || o == ui->dnDescCombo) && e->type() == QEvent::FocusOut)
    4. {
    5.  
    6. // Not sure how to emit returnPressed here
    7.  
    8. }
    9.  
    10. return QWidget::eventFilter(o, e);
    11. }
    To copy to clipboard, switch view to plain text mode 

    I just don't know how to emit a returnPressed() as he suggests. In the above code, dnNameCombo and dnDescCombo are the objectName properties of my editable QComboBox widgets.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Emulating Enter key with left mouse click

    Signals are protected so either you need to do some hacking to overcome protection or don't emit a signal but instead post an event to the widget or finally subclass the line edit and use the code fatjuicymole provided.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Emulating Enter key with left mouse click

    That worked. You guys are great. Thanks so much :-)

Similar Threads

  1. Replies: 1
    Last Post: 10th July 2009, 09:54
  2. combining Alt + Left Mouse Button
    By speedracer in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2009, 13:29
  3. Word at mouse click position in QGraphicsTextItem
    By pherthyl in forum Qt Programming
    Replies: 2
    Last Post: 3rd November 2008, 04:56
  4. Replies: 11
    Last Post: 15th July 2008, 13:11

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.