Let's say I have an application with a number of QGroupBoxes like so:
import sys
from PyQt4 import QtGui, QtCore
class Main(QtGui.QWidget):
# pylint: disable=too-many-statements
def __init__(self, main):
super(Main, self).__init__()
self.grid_layout = QtGui.QGridLayout()
self.line_edit = QtGui.QLineEdit()
self.grid_layout.addWidget(self.create_settings_group(), 0, 0, 2, 1)
self.push_button = QtGui.QPushButton("go", self)
self.grid_layout.addWidget(self.create_controls_group(), 0, 1)
self.setLayout(self.grid_layout)
main.setCentralWidget(self)
def create_settings_group(self):
group_box_settings = QtGui.QGroupBox(self)
group_box_settings.setTitle("group1")
grid = QtGui.QGridLayout()
grid.addWidget(self.line_edit, 0, 0)
group_box_settings.setLayout(grid)
return group_box_settings
def create_controls_group(self):
group_box_settings = QtGui.QGroupBox(self)
group_box_settings.setTitle("group2")
grid = QtGui.QGridLayout()
grid.addWidget(self.push_button, 0, 0, 1, 2)
group_box_settings.setLayout(grid)
return group_box_settings
class GUI(QtGui.QMainWindow):
def __init__(self):
super(GUI, self).__init__()
self.ui = Main(self)
self.show()
app = QtGui.QApplication(sys.argv)
ex = GUI()
app.exec_()
When I open my simple application I see that the cursor is blinking in the line edit. But I just want the push button in another group box to be highlighted and to have enter press connected to it? how do I do that? using self.push_button.setFocus() doesn't do anything.
You have to set the focus a moment after showing up for it you can use a QTimer::singleShot()
or QMetaObject::invokeMethod()
:
QTimer::singleShot()
...
self.push_button = QtGui.QPushButton("go", self)
self.grid_layout.addWidget(self.create_controls_group(), 0, 1)
self.push_button.setDefault(True)
QtCore.QTimer.singleShot(0, self.push_button.setFocus)
QMetaObject::invokeMethod()
...
self.push_button = QtGui.QPushButton("go", self)
self.grid_layout.addWidget(self.create_controls_group(), 0, 1)
self.push_button.setDefault(True)
QtCore.QMetaObject.invokeMethod(self.push_button,
"setFocus",
QtCore.Qt.QueuedConnection)