QTextEdit text color.



  • I have a logger that writes to the QTextEdit. I want to add a functionality that whenever the logger starts , it should check the textEdit has some text, if yes then the previous text color should change to grey , similar to QTextEdit functionality used in Qt Creator.

    I am able to achieve this using

    @if (!m_output->toPlainText().isEmpty())
    {
    QString textEditString = m_output->toHtml();

     m_output->clear();
     m_output->setHtml(QString("<font color=grey> %1 </font>").arg(textEditString));
    

    }@

    just want to know if there is any alternative way to achieve the same effect.



  • http://qt-project.org/doc/qt-4.8/QTextEdit.html

    Have you tried setTextColor?

    You could also use setPalette:
    http://qt-project.org/doc/qt-4.8/qwidget.html

    ... but I think it is recommended to use the html syntax instead.



  • Style sheets is another way...

    @
    QString styleSheet = "#m_output { color: rgb(0, 20, 50); }";
    m_output->setStyleSheet(styleSheet);
    @

    where your rgb color can be what ever you would like. Make sure you set your accessible name and your object name to the name described after your # sign in the style sheet string.

    so, where ever your constructor is that creates the button, immediately after creation, do:
    @
    m_output->setObjectName("m_output");
    m_output->setAccessibleName("m_output");
    @

    Then set the style sheet dynamically through code. You can customize the whole object with style sheets, color, shape, background color, ect.



  • [quote author="dvez43" date="1358191619"]Style sheets is another way...

    @
    QString styleSheet = "#m_output { color: rgb(0, 20, 50); }";
    m_output->setStyleSheet(styleSheet);
    @

    where your rgb color can be what ever you would like. Make sure you set your accessible name and your object name to the name described after your # sign in the style sheet string.

    so, where ever your constructor is that creates the button, immediately after creation, do:
    @
    m_output->setObjectName("m_output");
    m_output->setAccessibleName("m_output");
    @

    Then set the style sheet dynamically through code. You can customize the whole object with style sheets, color, shape, background color, ect.[/quote]

    Thanks dvez43,

    The problem with stylesheet is that once we set the stylesheet it reflects foreach text in textEdit, but my requirement is to change the previous text entry to grey and the currrent text added should have color which is specified/hardcoded.

    I am able to achieve the same behaviour, the issue is that i am first storing it to a string then clearing the textEdit and then adding the previous text again with grey color, which i think is not suitable.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.