在QPlainTextEdit()中禁用回车(按Enter键)

时间:2019-08-05 02:56:08

标签: python python-3.x pyqt pyqt5 qplaintextedit

这是我的示例:

Disable Return

文本区域是QPlainTextEdit()对象,因为我希望文本换行到第二行。我认为这是最好的小部件选择。

用户在此框中最多只能输入90个字符,因此我不需要较大的文本区域。

我想禁用按键,按Enter键(回车)。我可以使用它,但是它似乎很笨拙,我认为它不能跨平台(例如Mac)使用。

肯定有一种更好的方法来防止QPlainTextEdit对象中的回车键事件?

我当前的解决方案说明

下面,您会看到我正在检查是否发生IndexError,因为QPlainTextEdit框中没有内容时,last_value会抛出IndexError。然后,我得到了最后一个字符并询问它是否等于新行。如果是这样,我将重新设置文本而没有新行,并将光标移到末尾。

    def some_event(self):
        try:
            last_value = self.field.toPlainText()[-1]
            if last_value == '\n':
                print('You Pressed Enter!', repr(last_value))
                self.field.setPlainText(self.field.toPlainText()[:-1])
                self.field.moveCursor(QTextCursor.End)
        except IndexError:
            print('Index Error occurred')
            pass

完整代码最少工作示例:

from PyQt5.QtWidgets import (QWidget, QMainWindow, QGridLayout, QPushButton,
                             QApplication, QPlainTextEdit, QLabel)
from PyQt5.QtGui import QTextCursor

class BasicWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initWindow()

    def initWindow(self):
        self.setGeometry(400, 300, 400, 100)

        self.grid = QGridLayout()

        self.label = QLabel('Description Line 1')
        self.grid.addWidget(self.label, 0, 0)

        self.field = QPlainTextEdit()
        self.field.setMaximumHeight(40)

        self.field.textChanged.connect(self.some_event)

        #TODO how to disable enter/return key events in this field?
        self.grid.addWidget(self.field, 1, 0)

        self.button = QPushButton('Some Button')
        self.grid.addWidget(self.button)

        self.centralWidget = QWidget()
        self.centralWidget.setLayout(self.grid)
        self.setCentralWidget(self.centralWidget)

    def some_event(self):
        try:
            last_value = self.field.toPlainText()[-1]
            if last_value == '\n':
                print('You Pressed Enter!', repr(last_value))
                self.field.setPlainText(self.field.toPlainText()[:-1])
                self.field.moveCursor(QTextCursor.End)
        except IndexError:
            print('Index Error occurred')
            pass

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = BasicWindow()
    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:2)

一种选择是覆盖keyPressEventQPlainTextEdit方法:

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QTextCursor
from PyQt5.QtWidgets import (QWidget, QMainWindow, QGridLayout, QPushButton,
                             QApplication, QPlainTextEdit, QLabel)


class PlainTextEdit(QPlainTextEdit):
    def keyPressEvent(self, event):
        if event.key() in (Qt.Key_Return, Qt.Key_Enter):
            return
        super().keyPressEvent(event)


class BasicWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initWindow()

    def initWindow(self):
        self.setGeometry(400, 300, 400, 100)
        self.label = QLabel("Description Line 1")
        self.field = PlainTextEdit()  
        self.field.setMaximumHeight(40)   
        self.button = QPushButton("Some Button")

        self.centralWidget = QWidget()
        grid = QGridLayout(self.centralWidget)
        grid.addWidget(self.label, 0, 0)
        grid.addWidget(self.field, 1, 0)
        grid.addWidget(self.button)
        self.setCentralWidget(self.centralWidget)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    window = BasicWindow()
    window.show()
    sys.exit(app.exec_())

实现相同逻辑的另一个选项是使用eventFilter()

from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtGui import QTextCursor
from PyQt5.QtWidgets import (QWidget, QMainWindow, QGridLayout, QPushButton,
                             QApplication, QPlainTextEdit, QLabel)

class BasicWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initWindow()

    def initWindow(self):
        self.setGeometry(400, 300, 400, 100)
        self.label = QLabel("Description Line 1")
        self.field = QPlainTextEdit()  
        self.field.setMaximumHeight(40)   
        self.button = QPushButton("Some Button")

        self.field.installEventFilter(self)

        self.centralWidget = QWidget()
        grid = QGridLayout(self.centralWidget)
        grid.addWidget(self.label, 0, 0)
        grid.addWidget(self.field, 1, 0)
        grid.addWidget(self.button)
        self.setCentralWidget(self.centralWidget)

    def eventFilter(self, obj, event):
        if obj is self.field and event.type() == QEvent.KeyPress:
            if event.key() in (Qt.Key_Return, Qt.Key_Enter):
                return True
        return super().eventFilter(obj, event)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    window = BasicWindow()
    window.show()
    sys.exit(app.exec_())