简体   繁体   English

如何防止pyqt中的textEdit更改

[英]how to prevent textEdit in pyqt from changing

how to prevent textEdit in pyqt from changing when to many characters are entered? 如何防止pyqt中的textEdit更改为何时输入多个字符? can not find setting qt designer to limit number of characters, or limit auto resizing of box. 找不到设置qt设计器来限制字符数或限制框的自动调整大小。
tried: 尝试过:

self.ui.textEdit_45.setFixedSize(30,  30)

set the minimium and maximium to width 30, height 30 将最小值和最大值设置为宽度30,高度30
sizePolicy [Fixed, Fixed, 0, 0] sizePolicy [固定,固定,0、0]
did not work 不工作

if more than 2 characters are entered, the box gets 2 x's at top right and bottom left, and can not make out what is in the box. 如果输入的字符超过2个,则该框的右上角和左下角将有2个x,并且无法辨别出该框中的内容。

An idea would be to count the character the textEdit has QTextEdit.toPlainText() every time the text changes self.textEdit.textChanged.connect() . 一个想法是,每次文本更改self.textEdit.textChanged.connect()时,计算textEdit具有QTextEdit.toPlainText()的字符。 If the number of character makes the QTextEdit deforme, make appear like the input it's disabled by slicing the input. 如果字符数量使QTextEdit变形,则通过对输入进行切片,使make看起来像输入一样,它已被禁用。

MWE: MWE:

from PyQt4 import  QtGui, QtCore
import sys

class MWindow(QtGui.QMainWindow):

    TwoDigits = 2 # Maximum number of characters allowed

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setGeometry(300, 300, 250, 150)
        #define QTextEdit
        self.TE = QtGui.QTextEdit(self)
        self.TE.setGeometry(0,0, 30,30)

        self.TE.textChanged.connect(self.Limiter)

    def Limiter(self):
        NumChar = len( self.TE.toPlainText() )
        # according to geometry 2 characters is enough, so enter if over TwoDigits
        if NumChar > self.TwoDigits:
            Text = self.TE.toPlainText() # get text
            Text = Text[:self.TwoDigits] # slice first TwoDigits
            print(Text) # debugging
            self.TE.setPlainText(Text) # set sliced text
            self.TE.moveCursor(QtGui.QTextCursor.End) # move cursor to end, like if nothing happens

app = QtGui.QApplication([sys.argv])
win = MWindow()
win.show()
sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM