:
ありQTextEditの最大の長さを取得/設定するには直接のAPIはありませんが、あなたが接続して、この自分自身を扱うことができますスロットをcontentsChanged()信号に送信してから、toPlainText().length()に電話してどれくらい大きいかを調べます。上限に達している場合は、keyPressEvent()とkeyReleaseEvent()を再実装して、通常の文字に対して何もしないでください。付属のいくつかのコードを持っている
ます。またthis postに興味がある可能性がありは(うまくいけば、それはあなたのために働く):
txtInput = QPlainTextEdit()
QObject.connect(txtInput, SIGNAL("textChanged()"), txtInputChanged)
def txtInputChanged():
if txtInput.toPlainText().length() > maxInputLen:
text = txtInput.toPlainText()
text = text[:maxInputLen]
txtInput.setPlainText(text)
cursor = txtInput.textCursor()
cursor.setPosition(maxInputLen)
txtInput.setTextCursor(cursor)
別:
#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"));
}
}