Qtextedit with Different Text Colors (Qt/C++)

QTextEdit with different text colors (Qt / C++)

Use text formated as HTML, for example:

textEdit->setHtml(text);

where text, is a HTML formated text, contains with colored lines and etc.

text color QPlainTextEdit QT

If your goal is set the color of all text simply, you can use Qt StyleSheet!

The following example changes the background color to black and text color to red:

QPlainTextEdit *edit = new QPlainTextEdit(this);

//StyleSheet here
edit->setStyleSheet("QPlainTextEdit {background-color: black; color: red;}");

edit->appendPlainText("HELLO!");

EDIT: Without using StyleSheet:

QPlainTextEdit *edit = new QPlainTextEdit(this);

//Here we are using the HTML capabilities of Qt
//Converting the string using toHtmlEscaped to allow html special characters to be displayed
//Using blockSignals to not generate loop on text manipulation
//Using <pre> tag to allow multiple spaces

connect(edit, &QPlainTextEdit::textChanged, this, [=]{
const QString plainText = edit->toPlainText().toHtmlEscaped();

edit->blockSignals(true);
edit->clear();
edit->appendHtml("<p style=\"color:red;white-space:pre\">" + plainText + "</p>"
edit->blockSignals(false);
});

edit->appendPlainText("HELLO!");

How to Set custom text Color in QTextEdit?

I've had success changing text color of QLabel and QPlainTextEdit by changing the Palette:

QPalette pal;
pal.setColor(QPalette::Window, bgColor);
pal.setColor(QPalette::WindowText, fgColor);
pal.setColor(QPalette::Text, fgColor);
mylabel->setPalette(pal);

Set Color QTextEdit

The setTextColor you have should work fine - QColor(255,0,0) and Qt::red are the same thing. Tried it and works fine.
Sample Image

OR maybe style sheets would help

qApp->setStyleSheet("QTextEdit { color: red; } ");

https://doc.qt.io/Qt-5/stylesheet-syntax.html

QPlainTextEdit with multiple colours on a line

For these cases the simplest thing is to use HTML and insert the tags: <font color = "..."> </ font>

Example:

#include <QApplication>
#include <QDateTime>
#include <QPlainTextEdit>
#include <QTimer>
#include <QVBoxLayout>
#include <QWidget>

const std::map<QString, QColor> m_colours {{"red", QColor(Qt::red)},
{"blue", QColor(Qt::blue)},
{"green", QColor(Qt::green)}
};

class ProcessWidget: public QWidget{
Q_OBJECT
public:
ProcessWidget(QWidget *parent=nullptr):
QWidget(parent),
lay{this}
{
m_textedit.setReadOnly(true);
lay.addWidget(&m_textedit);
}
public slots:
void appendText(const QString & text){
QString html{text};
int j = 0;
bool start = true;
QString textColor;

while ((j = html.indexOf(QChar('\033'), j)) != -1) {
html.remove(j, 1);
QColor color;
for(auto & pair : m_colours){
if(html.mid(j).startsWith(pair.first)){
color = pair.second;
html.remove(j, pair.first.length());
}
}
if(start){
textColor = QString("<font color=\"%1\">").arg(color.name());
start = false;
}
else
textColor = QString("</font><font color=\"%1\">").arg(color.name());
html.insert(j, textColor);
j += 1+textColor.length();
}
html.append("</font>");
m_textedit.appendHtml(html);
}
private:
QPlainTextEdit m_textedit;
QVBoxLayout lay;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

ProcessWidget w;
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&w](){
QString text = QString("\033redDateTime: %1 \033blueDate:%2 \033greenTime:%3")
.arg(QDateTime::currentDateTime().toString())
.arg(QDate().currentDate().toString())
.arg(QTime::currentTime().toString());
w.appendText(text);
});
timer.start(1000);
w.show();
return a.exec();
}

#include "main.moc"

Sample Image

QTextEdit. In textChanged slot change colors for some words and restore

You are doing that wrong, creating big overhead. You should do this like that:

void Wnd::onTextChanged()
{
QTextDocument *doc = ui->textEdit->document();

// clears old formating
QTextCursor cursor(doc);
cursor.select(QTextCursor::Document);
cursor.setCharFormat(QTextCharFormat());

Q_FOREACH(QString word, wordsToColor) {
QTextCursor cursor = doc->find(word);

while(cursor.hasSelection()) {
cursor.setCharFormat(someTextCharFormat);
cursor = doc->find(word, cursor); // next word
}
}
}

It is also possible that you are trying to do something what is already solved: http://qt-project.org/doc/qt-4.8/richtext-syntaxhighlighter.html

How to change the font and color of QString?

your error message means that you are missing a ) because you have to escape the quotes when using them in a string literal (\" instead of ") i.e. do something like

QString redPart = QString("<span style=\" color:#ff0000;\">%1</span>").arg(">>>"); 

on the other hand, Qstring is a class that has nothing to do with widgets or guis at all, so properties like color, bold format etc arent defined there, so you need to use a widget or something that you can display to the user then if widget: you can set the stylesheet, if a QLabel: you can set the text using richtext.

Qt 5.3 QPlainTextEdit Change the QTextCursor color

You can use QTextCharFormat to set the color of the text in your QPlainTextEdit. Use the QTextCharFormat::setForeground to set the color.
Then use a stylesheet to change the color of the cursor by using the color property.

QPlainTextEdit *p_textEdit = new QPlainTextEdit;
p_textEdit->setStyleSheet("QPlainTextEdit{color: #ffff00; background-color: #303030;"
" selection-background-color: #606060; selection-color: #ffffff;}");
QTextCharFormat fmt;
fmt.setForeground(QBrush(QColor(255,255,255)));
p_textEdit->mergeCurrentCharFormat(fmt);


Related Topics



Leave a reply



Submit