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 :)
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 :)