QLineEdit: blinking cursor (caret) disappears. How to restore it?

huangapple go评论62阅读模式
英文:

QLineEdit: blinking cursor (caret) disappears. How to restore it?

问题

QLineEdit的闪烁光标(插入符号)在重写focusInEvent时消失。
如何恢复闪烁光标?有什么想法?

from PySide6.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton


class MyWindow(QMainWindow):

    def __init__(self):

        super().__init__()

        self.edt = QLineEdit(self)
        self.edt.move(10, 10)
        self.edt.focusInEvent = lambda event: self.on_focus_in_event(event)

        self.btn = QPushButton("OK", self)
        self.btn.move(10, 50)

    def on_focus_in_event(self, event):
        print("Doing something")
        self.edt.setFocus()


if __name__ == '__main__':
    app = QApplication()
    window = MyWindow()
    window.show()
    app.exec()

我尝试了一些与QLineEdit光标相关的方法,但无法解决问题。

英文:

QLineEdit blinking cursor (caret) disappears when focusInEvent is overridden.
How to restore the blinking cursor? Any idea?

from PySide6.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton


class MyWindow(QMainWindow):

    def __init__(self):

        super().__init__()

        self.edt = QLineEdit(self)
        self.edt.move(10, 10)
        self.edt.focusInEvent = lambda event: self.on_focus_in_event(event)

        self.btn = QPushButton("OK", self)
        self.btn.move(10, 50)

    def on_focus_in_event(self, event):
        print("Doing something")
        self.edt.setFocus()


if __name__ == '__main__':
    app = QApplication()
    window = MyWindow()
    window.show()
    app.exec()

I tried some methods related to QLineEdit cursor but I couldn't solve the problem.

答案1

得分: 0

以下是您要翻译的内容:

"The way you redirect the event handling is wrong. This is not the way we are supposed to do it in Qt. At least not when you want to be able to call also the original event handler of QLineEdit which takes care if showing the caret. In other words, you have overwritten the original event handler (which shows the caret) and it is not called at all.

If you want to do some action when the line edit gains focus, use event filters. This is exactly the perfect mechanism for your use case.

from PySide6.QtCore import QEvent
from PySide6.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton


class MyWindow(QMainWindow):

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

        self.edt = QLineEdit(self)
        self.edt.move(10, 10)
        # installing event filter
        self.edt.installEventFilter(self)  

        self.btn = QPushButton("OK", self)
        self.btn.move(10, 50)

    def eventFilter(self, watched, event):
        # handling focus-in event of the QLineEdit
        if watched == self.edt and event.type() == QEvent.Type.FocusIn:
            print("Doing something")
            # returning False means that the original event handler is still called so that the caret gets visible
            return False

        # canonical way of handling of all other events
        return super().eventFilter(watched, event)


if __name__ == '__main__':
    app = QApplication()
    window = MyWindow()
    window.show()
    app.exec()

"

英文:

The way you redirect the event handling is wrong. This is not the way we are supposed to do it in Qt. At least not when you want to be able to call also the original event handler of QLineEdit which takes care if showing the caret. In other words, you have overwritten the original event handler (which shows the caret) and it is not called at all.

If you want to do some action when the line edit gains focus, use event filters. This is exactly the perfect mechanism for your use case.

from PySide6.QtCore import QEvent
from PySide6.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton


class MyWindow(QMainWindow):

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

        self.edt = QLineEdit(self)
        self.edt.move(10, 10)
        # installing event filter
        self.edt.installEventFilter(self)  

        self.btn = QPushButton("OK", self)
        self.btn.move(10, 50)

    def eventFilter(self, watched, event):
        # handling focus-in event of the QLineEdit
        if watched == self.edt and event.type() == QEvent.Type.FocusIn:
            print("Doing something")
            # returning False means that the original event handler is still called so that the caret gets visible
            return False

        # canonical way of handling of all other events
        return super().eventFilter(watched, event)


if __name__ == '__main__':
    app = QApplication()
    window = MyWindow()
    window.show()
    app.exec()

huangapple
  • 本文由 发表于 2023年2月27日 04:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定