PDA

View Full Version : Emulating Enter key with left mouse click



wconstan
30th December 2009, 08:03
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:


void myClass::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
// DO SOMETHING HERE: perhaps something like myComboBox->lineEdit()->....
// ...
}
}

Thanks!

wysota
30th December 2009, 09:00
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.

wconstan
30th December 2009, 14:41
You can emit returnPressed() there or send a key event to fake return.

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

squidge
30th December 2009, 15:16
You mean something like:



void myClass::focusOutEvent()
{
returnPressed();
baseClass:focusOutEvent(); // call base class implementation
}

wconstan
30th December 2009, 15:27
Going with wysota's original suggestion, I've made headway:


bool MyClass::eventFilter(QObject *o, QEvent *e)
{
if ((o == ui->dnNameCombo || o == ui->dnDescCombo) && e->type() == QEvent::FocusOut)
{

// Not sure how to emit returnPressed here

}

return QWidget::eventFilter(o, e);
}

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.

wysota
30th December 2009, 15:38
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.

wconstan
30th December 2009, 16:16
That worked. You guys are great. Thanks so much :-)