pyqt4 QTextEdit-如何设置MaxLength?

我有一个多行QTextEdit绑定到数据库VARCHAR(2048)字段.

我想将用户输入的长度限制为最多2048个字符

QTextEdit没有像QLineEdit这样的setMaxLength(int)方法.

有人有什么建议吗?

self.editBox = QTextEdit()

谢谢

解决方法:

我在Qt Wiki上找到了this FAQ

There is no direct API to set/get a maximum length of a QTextEdit, but you can handle this yourself by connecting a slot to the 07001 signal and then call 07002 to find out how big it is. If it is up to the limit then you can reimplement 07003 and 07004 to do nothing for normal characters.

您可能也对this post感兴趣,该代码附加了一些代码(希望它对您有用):

#include <QtCore>
#include <QtGui>
#include "TextEdit.hpp"

TextEdit::TextEdit() : QPlainTextEdit() {
connect(this, SIGNAL(textChanged()), this, SLOT(myTextChanged()));
}

TextEdit::TextEdit(int maxChar) : QPlainTextEdit() {
this->maxChar = maxChar;
connect(this, SIGNAL(textChanged()), this, SLOT(myTextChanged()));
}

int TextEdit::getMaxChar() {
return maxChar;
}

void TextEdit::setMaxChar(int maxChar) {
this->maxChar = maxChar;
}

void TextEdit::myTextChanged() {
if (QPlainTextEdit::toPlainText().length()>maxChar) {
QPlainTextEdit::setPlainText(QPlainTextEdit::toPlainText().left(QPlainTextEdit::toPlainText().length()-1));
QPlainTextEdit::moveCursor(QTextCursor::End);
QMessageBox::information(NULL, QString::fromUtf8("Warning"),
QString::fromUtf8("Warning: no more then ") + QString::number(maxChar) + QString::fromUtf8(" characters in this field"),
QString::fromUtf8("Ok"));
}
}
上一篇:python-PyQt中透明QSplashScreen图像上的白色边框


下一篇:python-PyQT中的弹出式非模式嵌入式对话框