FabioG
18th June 2020, 07:45
I'm trying to implement a shared text editor using c++ and qt. I would like to highlight the text inserted from different users with different colors. I can do that but the problem is that than I can't reset the charFormat and all the subsequent text I wrote is hihghlighted
Code I use to highlight and change charFormat
QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
insertCursor.setPosition(std::stoi(pos));
insertCursor.insertText(character.c_str(), fmt);
fmt.setBackground(Qt::white);
insertCursor.setCharFormat(fmt);
some images to explain better
same file opened in 2 clients
13475
left client wrote and right client updated text with highlight
13476
right client wrote but remain highlighted
13477
I also tried to use 2 different cursors one with the previous charFormat and one with one highlight settings and
moving the cursor locking the anchor this way
QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
QTextCursor cursor(ui->textEdit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(fmt);
fmt.setBackground(Qt::white);
cursor.setCharFormat(fmt);
(this is a function so I pass end and begin as values)
I suppose the problem could be related to the anchor that doesn't follow anymore the cursor or the fact the cursor use the previous character charFormat when I type but I really can't find out how to solve this problem
thanks to everyone in advice
Code I use to highlight and change charFormat
QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
insertCursor.setPosition(std::stoi(pos));
insertCursor.insertText(character.c_str(), fmt);
fmt.setBackground(Qt::white);
insertCursor.setCharFormat(fmt);
some images to explain better
same file opened in 2 clients
13475
left client wrote and right client updated text with highlight
13476
right client wrote but remain highlighted
13477
I also tried to use 2 different cursors one with the previous charFormat and one with one highlight settings and
moving the cursor locking the anchor this way
QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
QTextCursor cursor(ui->textEdit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(fmt);
fmt.setBackground(Qt::white);
cursor.setCharFormat(fmt);
(this is a function so I pass end and begin as values)
I suppose the problem could be related to the anchor that doesn't follow anymore the cursor or the fact the cursor use the previous character charFormat when I type but I really can't find out how to solve this problem
thanks to everyone in advice