Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to change the color of text to blue where ever i get the word "class" in qt edittext , but i cant find any way to do it ,


i am new in qt , can anyone please help me
here is my code of file open

C++
void MainWindow::on_actionOpen_triggered()
{
    QString file = QFileDialog::getOpenFileName(this,"Qpen a file please");
    if(!file.isEmpty())
    {
        QFile sFile(file);
        if(sFile.open(QFile::ReadOnly | QFile::Text))
        {
            mFilename = file;
            QTextStream in(&sFile);
            QString text = in.readAll();
            sFile.close();
            ui->textEdit->setPlainText(text);
        }
    }
}
Posted

1 solution

This requires that the QTextEdit uses HTML text (also called RichText with Qt).

When loading a plain text file you must convert it to HTML and apply the required formats (e.g. by replacing "class" with "<font color=\"blue\">class</font>"). Then use setHtml() instead of setPlainText(). See the Qt documentation for the supported HTML styles (it is only a subset of CSS).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900