Hi,
i'm trying to do simple formating function with QTextCursor in QPlainTextEdit, but still don't understand why my example app crashes with error:
Starting C:\Qt\Projects\untitled-build-desktop\debug\untitled.exe...
ASSERT: "n" in file ..\..\include\QtGui/private/../../../src/gui/text/qfragmentmap_p.h, line 291
C:\Qt\Projects\untitled-build-desktop\debug\untitled.exe exited with code 3
Starting C:\Qt\Projects\untitled-build-desktop\debug\untitled.exe...
ASSERT: "n" in file ..\..\include\QtGui/private/../../../src/gui/text/qfragmentmap_p.h, line 291
C:\Qt\Projects\untitled-build-desktop\debug\untitled.exe exited with code 3
To copy to clipboard, switch view to plain text mode
I have this simple code:
MainWindow
::MainWindow(QWidget *parent
){
this->edit=new QPlainTextEdit(this);
this->setCentralWidget(this->edit);
connect(edit->document(),SIGNAL(contentsChange(int,int,int)),this,SLOT(contentsChange(int,int,int)));
}
void MainWindow::contentsChange(int position,int charsRemoved,int charsAdded) {
if (this->edit->toPlainText().length()>10) {
charFormat.setFontItalic(true);
curA->beginEditBlock();
if (curA->hasSelection()) {
curA->mergeCharFormat(charFormat);
curA->clearSelection();
}
curA->endEditBlock();
delete curA;
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->edit=new QPlainTextEdit(this);
this->setCentralWidget(this->edit);
connect(edit->document(),SIGNAL(contentsChange(int,int,int)),this,SLOT(contentsChange(int,int,int)));
}
void MainWindow::contentsChange(int position,int charsRemoved,int charsAdded) {
if (this->edit->toPlainText().length()>10) {
QTextCursor *curA=new QTextCursor(this->edit->document()->findBlock(0));
QTextCharFormat charFormat;
charFormat.setFontItalic(true);
curA->setPosition(0,QTextCursor::MoveAnchor);
curA->setPosition(4,QTextCursor::KeepAnchor);
curA->beginEditBlock();
if (curA->hasSelection()) {
curA->mergeCharFormat(charFormat);
curA->clearSelection();
}
curA->endEditBlock();
delete curA;
}
}
To copy to clipboard, switch view to plain text mode
I'm trying to create simple function which formats specified number of characters from specified position (something like setFormat(int position,int length)) with QTextCursor, but my function is crashing because of mentioned error, so i have prepared simple example to show you way how i'm trying to do it.
Bookmarks