PDA

View Full Version : Textedit - can't reset charFormat after highlight



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

FabioG
21st June 2020, 07:44
There's anyone that can help me?

I can add a video to explain better the problem and maybe reword the question :)


https://youtu.be/xDSQ0xgVcQQ



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:


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


HighlightText function


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

}

ChrisW67
27th June 2020, 07:07
I generated a basic QMainWindow project with aQTextEdit as the central widget. This code caches the formats and generates a stream of foreign text that switches from plain to highlighted and back without issue. run it an type continously for a few seconds. Useful?


#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTimer>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_timer(nullptr)
{
ui->setupUi(this);

m_base = ui->textEdit->textCursor().charFormat();
m_highlighted = m_base;
m_highlighted.setBackground(Qt::yellow);

m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &MainWindow::writeForeignText);
m_timer->setInterval(5000);
m_timer->start();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::writeForeignText()
{
QTextCursor cursor = ui->textEdit->textCursor();
cursor.setCharFormat(m_highlighted);
cursor.insertText("Plugh");
cursor.setCharFormat(m_base);
ui->textEdit->setTextCursor(cursor);
}