Reputation: 23
I'm writing some code using Python 3.4, Qt 4.8.6 trough PySide 1.2.1.
I'm trying to get a custom Text Editor to work by using a QTextEdit and some QPushButton's, problem is: if there are QPushButtons in the Editor, QTextEdit doesn't get focused at window initialization, even if a explicit call to QTextEdit.setFocus() is made. If the buttons aren't included, all Just Works (TM), here is some code:
#!/usr/bin/env python3
import sys
from PySide.QtGui import *
from PySide.QtCore import *
app = QApplication(sys.argv)
class MyEditor(QHBoxLayout):
def __init__(self):
super(MyEditor, self).__init__()
self.add_buttons()
self.add_editor()
def add_buttons(self):
self.buttons_layout = QVBoxLayout()
self.addLayout(self.buttons_layout)
self.b1 = QPushButton('1')
self.b2 = QPushButton('2')
for b in (self.b1, self.b2):
self.buttons_layout.addWidget(b)
def add_editor(self):
self.editor = QTextEdit()
self.addWidget(self.editor)
self.editor.setFocus()
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.editor = MyEditor()
self.setLayout(self.editor)
self.show()
window = Window()
app.exec_()
sys.exit()
But if I don't include the Buttons (by commenting out the call to self.add_buttons), the QTextEdit gets focused just fine:
#!/usr/bin/env python3
import sys
from PySide.QtGui import *
from PySide.QtCore import *
app = QApplication(sys.argv)
class MyEditor(QHBoxLayout):
def __init__(self):
super(MyEditor, self).__init__()
# self.add_buttons() <- Now focus works, but no buttons :(
self.add_editor()
def add_buttons(self):
self.buttons_layout = QVBoxLayout()
self.addLayout(self.buttons_layout)
self.b1 = QPushButton('1')
self.b2 = QPushButton('2')
for b in (self.b1, self.b2):
self.buttons_layout.addWidget(b)
def add_editor(self):
self.editor = QTextEdit()
self.addWidget(self.editor)
self.editor.setFocus()
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.editor = MyEditor()
self.setLayout(self.editor)
self.show()
window = Window()
app.exec_()
sys.exit()
I deeply researched the PySide docs, google and etc. but no answer could be found, any ideas? Thanks in advance.
PS.: Sorry about any mistakes in my English, I'm not a native of this language.
Upvotes: 1
Views: 881
Reputation: 10865
Delaying the call to setFocus
on the QTextEdit to after setLayout
on QWidget has finished solves the problem. So I guess that setLayout changes the focus and one has to manually reset it afterwards. It might have to do with the tab order (see first answer in Set QLineEdit focus in Qt).
Here is your example with buttons and focus on the editor widget:
from PySide.QtGui import *
class MyEditor(QHBoxLayout):
def __init__(self):
super(MyEditor, self).__init__()
self.add_buttons()
self.add_editor()
def add_buttons(self):
self.buttons_layout = QVBoxLayout()
self.addLayout(self.buttons_layout)
self.b1 = QPushButton('1')
self.b2 = QPushButton('2')
for b in (self.b1, self.b2):
self.buttons_layout.addWidget(b)
def add_editor(self):
self.editor = QTextEdit()
self.addWidget(self.editor)
def set_focus(self):
self.editor.setFocus()
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.editor = MyEditor()
self.setLayout(self.editor)
self.editor.set_focus()
self.show()
app = QApplication([])
window = Window()
app.exec_()
Upvotes: 1