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
) , 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);
connect(m_timer, &QTimer::timeout, this, &MainWindow::writeForeignText);
m_timer->setInterval(5000);
m_timer->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::writeForeignText()
{
cursor.setCharFormat(m_highlighted);
cursor.insertText("Plugh");
cursor.setCharFormat(m_base);
ui->textEdit->setTextCursor(cursor);
}
#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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks