qt qtextedit 限制富文本复制 限制字符

qtextedit 支持很多,比如富文本等。但有时候只要想普通字符,并且限制字符数量。

	ui->description_edit->setFixedHeight(80);
	ui->description_edit->setAcceptRichText(false);
    connect(ui->description_edit, &QTextEdit::textChanged, this, [ = ] {
        QString textContent = ui->description_edit->toPlainText();
        qint32 length = textContent.count();
        qint32 maxLength = 80; // 最大字符数
        if(length > maxLength) {
            int position = ui->description_edit->textCursor().position();
            QTextCursor textCursor = ui->description_edit->textCursor();
            textContent.remove(position - (length - maxLength), length - maxLength);
            ui->description_edit->setText(textContent);
            textCursor.setPosition(position - (length - maxLength));
            ui->description_edit->setTextCursor(textCursor);
        }
    });
发布了135 篇原创文章 · 获赞 68 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/a15005784320/article/details/103904086
今日推荐