New Moon
New Moon

Reputation: 817

How to move the focus from a QLineEdit to a QTableView editable cell

I am using a QMainWindow with few QLineEdits and with some QPushButtons in it. When the focus is in a QLineEdit (if I type something in the QLineEdit) and if I press the F5 key, I want to show a QDialog.

That QDialog contains a QTableView. My question is, when I press the F5 key, I want to move the focus from the QLineEdit to the QTableView's cell. How can I achieve this?

Upvotes: 0

Views: 1064

Answers (1)

Daniel Hedberg
Daniel Hedberg

Reputation: 5817

Subclass QLineEdit and override keyPressEvent() to detect when the F5 key is pressed, or install an event filter on the QLineEdit.

If you create and show the dialog during the key event processing the dialog will automatically receive a focus in event and the first widget in the dialog that accepts focus will be the widget in focus. So either let the QTableView be the first widget, or explicitly give the focus to it using setFocus().

If the dialog is already constructed or is a non-modal dialog which is already open, you need a pointer to the dialog so that you can show it/give it the focus when the F5 key is pressed.

If you want to move to a certain cell in the QTableView you of course also need to know the cell associated with your QLineEdit.

Upvotes: 1

Related Questions