PDA

View Full Version : Need idea how to implement my own SetFormat function



karel007
6th February 2011, 11:13
Hi,
i would like to ask you for help. I'm creating simple editor working with large texts. I want to have there simple formating possibility like change color of text. I'm using QPlainTextEdit (because it's faster then QTextEdit) as main input widget and it should have simple possibility to change color of existing text. Function should look like this:


//position - char's position in text where we want to start with formatting
//length - to how many characters we want to apply format
//format - charformat which we want to apply to characters from position to position+length
SetFormat(int position,int length,QTextCharFormat &format) {
//implementation
}


If user push some button, i'll call this function and pass there parameters to make already written text colorful or something. This function already exists in QSyntaxHighlighter, but there is lot of limitation which are not acceptable for me.
I have tried something, but it's crashing (i have asked already in this forum why it's crashing,but i can't get there for replies, because there is some error in link or something):



void MainWindow::SetFormat(int position,int length,QTextCharFormat &format) {
QTextCursor *curA=new QTextCursor(this->edit->document());//this->edit is QTextPlainEdit class
curA->setPosition(position,QTextCursor::MoveAnchor);
curA->setPosition(length,QTextCursor::KeepAnchor);
curA->beginEditBlock();
if (curA->hasSelection()) {
curA->mergeCharFormat(format);
curA->clearSelection();
}
curA->endEditBlock();
}


Hope, there are not many mistakes in my first english text :)

wysota
6th February 2011, 22:32
Make sure "edit" and "edit->document()" return valid values (not null and properly initialized with valid objects). Also don't allocate the cursor on heap, currently you have a memory leak. You can safely allocate the cursor on stack.

ChrisW67
6th February 2011, 22:59
Your original thread is quite accessible to me:
http://www.qtcentre.org/threads/38456-App-crashes-on-error-ASSERT-quot-n-quot-in-file-..%5C..%5Cinclude%5CQtGui-private-..-..-..-src-gui

As far I can tell, a QPlainTextEdit and its simplified layout engine doesn't support anything except unformatted plain text and behaves poorly when you try to merge a format into it.

karel007
7th February 2011, 19:03
OK, so i little changed my original code as you adviced so now i have


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->edit=new QPlainTextEdit(this);
this->setCentralWidget(this->edit);
connect(edit->document(),SIGNAL(contentsChange(int,int,int)),thi s,SLOT(contentsChange(int,int,int)));
}

void MainWindow::SetFormat(int position,int length,QTextCharFormat &format) {
if (this->edit->document()!=NULL) {
QTextCursor curA(this->edit->document());
curA.setPosition(position,QTextCursor::MoveAnchor) ;
curA.setPosition(length,QTextCursor::KeepAnchor);
curA.beginEditBlock();
if (curA.hasSelection()) {
curA.mergeCharFormat(format);
curA.clearSelection();
}
curA.endEditBlock();
} else {
qDebug("qplaintextedit->document() is null.");
}
}

void MainWindow::contentsChange(int position,int charsRemoved,int charsAdded) {
if (this->edit->toPlainText().length()>10) {
QTextCharFormat charFormat;
charFormat.setFontItalic(true);
this->SetFormat(0,3,charFormat);
}
}



But app is still crashing with


ASSERT: "n" in file ..\..\include\QtGui/private/../../../src/gui/text/qfragmentmap_p.h, line 291


I think ChrisW67 is true about simple layout text engine, but if u use QSyntaxHighlighter with QPlainTextEdit, then u can use colored text, so i think there has to be way how to do it.

P.S. I would like to delete my previous thread, but can't. Thread is accessible via link in prev.post, but not via threads list using chrome hmmm.