Error in input content of plainTextEdit component in Qt

Question

case: This is just a simple demo of the program, but I found a problem that when I quickly enter content (which seems to be only in Chinese) within the QplainTextEdit component on the program’s gui interface, the following output is produced:

QTextCursor::setPosition: Position '12' out of range
QTextCursor::setPosition: Position '16' out of range
QTextCursor::setPosition: Position '110' out of range

As shown in the following figure
enter image description here
The Chinese content in the picture was typed randomly by me in the case of Pinyin input method, which will trigger the bug at some point.
So what is this problem and how do I solve it?

Code Section for dialog.h


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

QT_BEGIN_NAMESPACE
namespace Ui {
class Dialog;
}
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private slots:
    void on_chkBoxUnder_clicked(bool checked);

    void on_chkBoxItatlic_clicked(bool checked);

    void on_chkBoxBold_clicked(bool checked);

    void do_setFontColor();

private:
    Ui::Dialog *ui;

};
#endif // DIALOG_H

Code Section for dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->radioBlack->setChecked(true);

    connect(ui->radioBlack,&QRadioButton::clicked,this,&Dialog::do_setFontColor);
    connect(ui->radioBlue,&QRadioButton::clicked,this,&Dialog::do_setFontColor);
    connect(ui->radioRed,&QRadioButton::clicked,this,&Dialog::do_setFontColor);
}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_chkBoxUnder_clicked(bool checked)
{
    QFont font = ui->plainTextEdit->font();
    font.setUnderline(checked);
    ui->plainTextEdit->setFont(font);
}


void Dialog::on_chkBoxItatlic_clicked(bool checked)
{
    QFont font = ui->plainTextEdit->font();
    font.setItalic(checked);
    ui->plainTextEdit->setFont(font);
}


void Dialog::on_chkBoxBold_clicked(bool checked)
{
    QFont font = ui->plainTextEdit->font();
    font.setBold(checked);
    ui->plainTextEdit->setFont(font);
}

void Dialog::do_setFontColor()
{
    QPalette plet = ui->plainTextEdit->palette();
    if(ui->radioBlack->isChecked())
        plet.setColor(QPalette::Text,Qt::black);
    else if(ui->radioRed->isChecked())
        plet.setColor(QPalette::Text,Qt::red);
    else
        plet.setColor(QPalette::Text,Qt::blue);

    ui->plainTextEdit->setPalette(plet);
}

Code Section for main.cpp


#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

I tried asking copilot, but it didn’t solve the problem. Here are the two solutions it gave:

  1. Use signals and slots: Connect the textChanged signal of QPlainTextEdit to a slot function, and update the cursor position in the slot function. This ensures that the cursor position is updated correctly every time the text changes。
//I use QDesigner to add slot functions automatically    
void Dialog::on_plaintextedit_textchanged()
{
   QTextCursor cursor = plainTextEdit->textCursor();

    cursor.movePosition(QTextCursor::End);

    plainTextEdit->setTextCursor(cursor);
}
  1. Delay cursor positioning: If you are writing an auto-complete or real-time error detection feature, you may want to delay cursor positioning for a short time after text changes. You can use QTimer to achieve this delay。
//added a new function
void Dialog::updateCursorPosition()
{
    QTextCursor cursor = plainTextEdit->textCursor();
    cursor.setPosition(newPosition);
    plainTextEdit->setTextCursor(cursor);
}
//and add the following to the slot function
void Dialog::on_plaintextedit_textchanged()
{
    //delay calling updateCursorPosition () with QTimer
    QTimer::singleShot(100, this, SLOT(updateCursorPosition()));
}

But it didn’t work out.
When I quickly type something into the plaintexteidt component on the gui interface (which seems to be only in Chinese), qtcreator gives an error like: QTextCursor::setPosition: Position ’97’ out of range.

New contributor

TheFool is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật