<meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="border-collapse: collapse; color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 13px; ">Hello everybody,<div>
<br></div><div>In my PyQt4 application I have a main window with a QDockWidget which contains a widget composed by a QLineEdit and a QTreeView, which is used to launch the windows in the application.</div><div><br></div><div>
I also have a QAction, with a global application shortcut, associated to the QLineEdit mentioned before. The intention here is as the user starts to open multiple windows he will be using this shortcut to bring the launcher dock widget to front and have the keyboard focus set to the QLineEdit, making the navigation easier.</div>
<div><br></div><div>The problem I am having is when I hit the shortcut from another window in the application it brings me the main window and the dock widget to front, but it doesn't set focus to the QLineEdit. I have tried different possible solutions without success so far. The issue with the parenthood chain seems to be resolved, since the qApp.activeWindow() is the main window and it is parent of the QDockWidget which in its turn is the parent of the QLineWidget's parent. I also tried to obtain the previously focused widget and clear its focus, before setting focus to the QLineEdit. I also tried to play up with the focus reasons and even tried to simulate the mouse click event in the area of the QLineEdit. Unfortunately I haven't had the luck to get it working in any way.</div>
<div><br></div><div>Here is the code snippet of the action callback:</div><div><br></div><div><code></div><div><div><br></div><div>def on_action_search_triggered(self):</div><div> print 'inside the callback'</div>
<div> self.ed_search.setFocus(Qt.ActiveWindowFocusReason)</div><div><br></div><div> from bbm.main_window import MainWindow</div><div> main_window = MainWindow.get_singleton()</div><div> main_window.activateWindow()</div>
<div> main_window.raise_()</div><div> qApp.processEvents()</div><div> </div><div> focused_widget = qApp.focusWidget()</div><div> focused_widget.clearFocus()</div><div> </div><div> qApp.setActiveWindow(main_window)</div>
<div> </div><div> print self.ed_search.parent().parent().parent()</div><div> print qApp.activeWindow()</div><div> self.ed_search.setFocus(Qt.ActiveWindowFocusReason)</div><div> </div><div> qApp.processEvents()</div>
<div> print qApp.focusWidget()</div></div><div><br></div><div></code></div><div><div><br></div></div><div>You can see still on the code snippet above that I am still trying to force the application to give the focus the QLineEdit I want. But when that code gets to be executed I got the following output:</div>
<div><br></div><div><output></div><div><div>inside the callback</div><div><bbm.main_window.MainWindow object at 0x2d52830></div><div><bbm.main_window.MainWindow object at 0x2d52830></div><div>None</div></div>
<div><div></output></div><div><br></div><div><br></div><div>Note in the end the 'None' means that none widget currently is focused in the application.</div><div><br></div><div>I couldn't find a solution for this on Google, StackOverflow, etc. Can anybody please turn on the lights? :-)</div>
<div><br></div><div>Thank you very much for your attention.</div><div><br></div><div>Kindest regards,</div><div><br></div><div>Gabriel Reis</div></div></span>