0

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?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
New Moon
  • 787
  • 6
  • 21
  • 35
  • Can you please include the code of your previous attempts to set the focus to the specific cell and tell us what exactly goes wrong with it? – Bowdzone Apr 28 '15 at 12:31

1 Answers1

1

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.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
  • Thanks for your suggestion Daniel Hedberg. That helped me alot. I have another question. How the focus will change if i use a QWidget instead of a QDialog. I know QDialog automatically recieves the focus. But how about for a QWidget? – New Moon Apr 29 '15 at 13:34
  • You need to have a pointer to the widget so that you can give it focus by calling widget->setFocus(). If you found my answer helpful, feel free to accept it and/or upvote it. Thanks. – Daniel Hedberg Apr 29 '15 at 14:12
  • Your answer was really helpful and i tried and it works, When i close the child widget i want to revert back my focus to the QLineEdit (but this doesn't works anymore). – New Moon Apr 30 '15 at 09:03