Qt通过PySide: 如果窗口中有一个按钮,QTextEdit在窗口创建时不获得焦点

1 投票
1 回答
840 浏览
提问于 2025-04-18 07:49
I'm writing some code using Python 3.4, Qt 4.8.6 trough PySide 1.2.1.

我正在尝试通过使用QTextEdit和一些QPushButton来制作一个自定义文本编辑器,问题是:如果编辑器里有QPushButton,QTextEdit在窗口初始化时就无法获得焦点,即使我明确调用了QTextEdit.setFocus()。如果不包含按钮,一切就正常工作(TM),以下是一些代码:

问题代码

#!/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()

但是如果我不包含按钮(通过注释掉self.add_buttons的调用),QTextEdit就能正常获得焦点:

#!/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()

我深入研究了PySide的文档、谷歌等,但没有找到答案,有什么想法吗?谢谢!

PS:抱歉我的英语有任何错误,我不是这门语言的母语者。

1 个回答

1

在QTextEdit上调用setFocus的时间要等到QWidget的setLayout完成之后,这样就能解决问题。我猜是因为setLayout会改变焦点,所以之后需要手动重新设置一下焦点。这可能和标签顺序有关(可以看看设置QLineEdit焦点的第一个回答)。

下面是一个包含按钮和编辑器控件焦点的示例:

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_()

撰写回答