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:
- 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);
}
- 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.
TheFool is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.