Solved User-Defined Action for a QListWidget on Enter Keystroke
-
I have created a subclass of QListWidget called ClassList to list a number of items. These items are selectable and I created a menu for performing an action using ClassList::onShowContextMenu, which connects to a slot that I have called ClassList::onEndit. All this actually works perfectly fine.
Now, I would like to add the possibility of performing the action implemented via slot ClassList::onEdit when the Enter/Return key is pressed. How can I realize this? I have found some examples with event filters but I don't really understand how they would work for my case where the action is realized by a slot that I defined myself. Any suggestions?
-
How about reimplementing key press event for list widget ? Or create another object and install it event filter. If event is return keyevent call your on edit function. I can provide u sample if u want.
-
Reimplementing key press event does not work for the Enter key, so I will need an event filter. I would be very pleased with an example that fits to my context
-
@ModelTech said in User-Defined Action for a QListWidget on Enter Keystroke:
Reimplementing key press event does not work for the Enter key
please show your implementation.
When exactly do you want the ENTER/RETURN key to be triggered? I mean in which state is the listwidget at this time? -
Ok. I was not overriding the correct method. I reimplemented keyPressEvent while I should have reimplemented the generic event method. Now I have found a working solution :)
-
@ModelTech
Actually the base implementation of the generic event handler forwards the events to the specific event handlers (simply for convenience reasons). So the issue was somewhere else.
But at least its working now ;) -
Hmm, yes I now also got it to work with keyPressEvent. So, I must have typed something wrongly, I just can't remember what the difference was. Anyway, it now works with keyPressEvent which requires less code than reimplementing event :)
-
You can make it work with re-implementing event(..)
MyListWidget is inherited from QListWidgetbool MyListWidget::event(QEvent *e)
{
if (e->type() == QEvent::KeyPress){
QKeyEvent e1 = static_cast<QKeyEvent>(e);
if (e1->key()== Qt::Key_Return){
this->callMe();
return true;
}
}
return QWidget::event(e);
}