Qt-QTextEdit限制输入个数

相关资料:

https://blog.csdn.net/xiezhongyuan07/article/details/80611363?utm_source=blogxgwz6    原作

https://download.csdn.net/download/zhujianqiangqq/13756816    代码包下载

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += 
19     main.cpp 
20     mainwindow.cpp
21 
22 HEADERS += 
23     mainwindow.h
24 
25 FORMS += 
26     mainwindow.ui
27 
28 TARGET = ../../QtTreeViewCheckable/bin/QtTreeViewCheckable
29 
30 # Default rules for deployment.
31 qnx: target.path = /tmp/$${TARGET}/bin
32 else: unix:!android: target.path = /opt/$${TARGET}/bin
33 !isEmpty(target.path): INSTALLS += target
34 
35 RESOURCES += 
36     resource.qrc
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QStandardItemModel>
 6 #include <QDebug>
 7 #include <QMessageBox>
 8 #include <QFileSystemModel>
 9 #include <QException>
10 #include <QButtonGroup>
11 
12 QT_BEGIN_NAMESPACE
13 namespace Ui { class MainWindow; }
14 QT_END_NAMESPACE
15 
16 class MainWindow : public QMainWindow
17 {
18     Q_OBJECT
19 
20 public:
21     MainWindow(QWidget *parent = nullptr);
22     ~MainWindow();
23 private slots:
24     void on_textEdit_textChanged();
25 
26 private:
27     Ui::MainWindow *ui;
28 };
29 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     setWindowTitle(QStringLiteral("QTextEdit限制输入个数"));
10 }
11 
12 MainWindow::~MainWindow()
13 {
14     delete ui;
15 }
16 
17 void MainWindow::on_textEdit_textChanged()
18 {
19     const int MAX_SIZE =10;
20     QString textContent = ui->textEdit->toPlainText();
21     int length = textContent.count();
22     int maxLength = MAX_SIZE; // 最大字符数
23     if(length > maxLength)
24     {
25         int position = ui->textEdit->textCursor().position();
26         QTextCursor textCursor = ui->textEdit->textCursor();
27         textContent.remove(position - (length - maxLength), length - maxLength);
28         ui->textEdit->setText(textContent);
29         textCursor.setPosition(position - (length - maxLength));
30         ui->textEdit->setTextCursor(textCursor);
31     }
32     length = textContent.count();
33     ui->label->setText(QString("%1/%2").arg(MAX_SIZE-length).arg(MAX_SIZE));
34 }
View Code
原文地址:https://www.cnblogs.com/FKdelphi/p/14174446.html