【问题标题】:How to bring keyboard focus to QTextEdit in PyQt?如何将键盘焦点带到 PyQt 中的 QTextEdit?
【发布时间】:2016-08-03 09:31:15
【问题描述】:

我在我的 PyQt 用户界面中插入了一个简单的 QTextEdit 小部件。当用户想要在该小部件中输入文本时,他必须单击它。我的程序应该能够在某些情况下自动实现这一点,这样用户就可以开始在 QTextEdit 小部件中输入文本,而无需单击它。

我已经到了某个地方,但问题仍然没有完全解决。当我的程序调用focus() 函数时,光标将在最后一行的末尾开始闪烁。但是在键盘上键入不会插入任何文本。

    class myTextField(QPlainTextEdit):

        def __init__(self):
            super(myTextField, self).__init__()
            ...


        def focus(self):
            self.focusInEvent(QFocusEvent( QEvent.FocusIn ))
            # Now the cursor blinks at the end of the last line.
            # But typing on your keyboard doesn't insert any text.
            # You still got to click explicitly onto the widget..

        ...

    ###

非常感谢任何帮助:-)

【问题讨论】:

    标签: python python-3.x pyqt pyqt5


    【解决方案1】:

    使用setFocus() 方法。

    def focus(self):
        self.setFocus()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多