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:
Trouble 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.