There's anyone that can help me?
I can add a video to explain better the problem and maybe reword the question 
How can I change properly the charFormat of my text edit runtime?
And which is the best way to highlight a text?
also I modified the code a bit
Insert function:
insertCursor.setPosition(std::stoi(pos));
ui->textEdit->setTextCursor(insertCursor);
ui->textEdit->insertPlainText(character.c_str());
HighlightText(user, std::stoi(pos), std::stoi(pos) + 1);
fmt.setBackground(Qt::white);
insertCursor.setCharFormat(fmt);
insertCursor.
setPosition(std
::stoi(pos
) + 1,
QTextCursor::MoveAnchor);
QTextCharFormat fmt;
insertCursor.setPosition(std::stoi(pos));
ui->textEdit->setTextCursor(insertCursor);
ui->textEdit->insertPlainText(character.c_str());
HighlightText(user, std::stoi(pos), std::stoi(pos) + 1);
fmt.setBackground(Qt::white);
insertCursor.setCharFormat(fmt);
insertCursor.setPosition(std::stoi(pos) + 1, QTextCursor::MoveAnchor);
To copy to clipboard, switch view to plain text mode
HighlightText function
void MainWindow::HighlightText(std::string user, int begin, int end){
fmt.setBackground(Qt::yellow);
cursor.setCharFormat(fmt);
}
void MainWindow::HighlightText(std::string user, int begin, int end){
QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);
QTextCursor cursor(ui->textEdit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(fmt);
}
To copy to clipboard, switch view to plain text mode
Bookmarks