Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
5.12.9, 6.3.0
-
None
Description
Firstly, create a new QTextDocument object pointer, and the QTextCursor object retrieves the mouse position of the pointer document. Then add a QTextDocument object to the QTextEdit class. Finally, use a signal slot to print the parameters in QTextDocument:: contentsChange. When the document is inserted in English, the positional parameters are updated normally. Insert Chinese position without updating.
//代码占位符 ========================================== mainwindow.cpp ========================================= #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QTextEdit> #include <QTextDocument> #include <QTextCursor> #include <QMainWindow> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QTextDocument *doc = new QTextDocument; QTextCursor cur(doc); QTextEdit *edit = new QTextEdit(this); connect(doc,&QTextDocument::contentsChange,this,&MainWindow::DocContentsChange); edit->setDocument(doc); setCentralWidget(edit); } MainWindow::~MainWindow() { delete ui; } void MainWindow::DocContentsChange(int aFrom, int aCharsRemoves, int acharsAdded) { qDebug() << "aFrom" << aFrom << "aCharsRemoves" << aCharsRemoves << "acharsAdded" << acharsAdded; } ============================================ mainwindow.h ============================================= #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void DocContentsChange(int aFrom,int aCharsRemoves,int acharsAdded);private: Ui::MainWindow *ui; };#endif // MAINWINDOW_H ========================================== main.cpp ========================================== #include "mainwindow.h" #include <QApplication>int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }