[SOLVED] QTextEdit setTextColor() problem



  • Hi,

    I've got a suspicious problem with my QTextEdit. I set the text color to red:
    @QTextEdit *edit = new QTextEdit(this);
    edit->setTextColor(QColor(255,0,0));@
    When I run program and type anything into it, it appears red, BUT any time I delete the input till begin (means deleting the whole input from last character right to first character left) and try typing something new --> it appears in standard black color!
    What's going on there? That only happens when I delete the whole input!
    Example:
    I type "hello" into the textedit --> hello appears red.
    Then I delete the last two characters ("lo") and retype anything new, it is also red like it should be.
    However, as soon as I delete the whole word "hello" and try to type something new --> text color is black...

    What is wrong there?



  • Hi.
    yeah in first view it looks strange and question come to mind why so?
    but the nature of this slot setTextColor(QColor) is first time remains as provided color like red.you can see the cursor that blinks remains black not red.

    but if you want you want to foreground color like red for always then use CSS.

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

    after applying the above CSS you will see cursor gets blink red not black..
    hope this helps.



  • Hi,
    you can use signal/slot connection (textChanged() signal and setTextColor(QColor) slot). For example:
    textedit.h
    @class TextEdit : public QTextEdit
    {
    Q_OBJECT

    public:
    TextEdit(QWidget *parent = 0);

    public slots:
    void setTextColor();
    };
    @

    textedit.cpp
    @TextEdit::TextEdit(QWidget *parent)
    : QTextEdit(parent)
    {
    QTextEdit::setTextColor(QColor(255, 0, 0));
    connect(this, SIGNAL(textChanged()), this, SLOT(setTextColor()));
    }

    void TextEdit::setTextColor()
    {
    QTextEdit::setTextColor(QColor(255, 0, 0));
    }
    @

    main.cpp
    @int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    TextEdit edit;
    edit.show();
    return app.exec();
    }
    @



  • Hi again,
    thank you both for your fast support!
    @lamSumit:
    That is an interesting idea to use setStyleSheet()! I will see whether I can use it anyway...

    @Vovasty:
    That's almost the way I go now. I found out, that setTextColor(QColor) only changes the CURRENT format, so what I did now was catching the classes signal "currentCharFormatChanged" and connecting it to a slot where it set's text color again :)



  • Hi.
    That's fine.
    i seem that your problem is solved Please update thread title as [SOLVED]
    Thanks.



  • Good idea, did not know that I can edit topic title once I posted the first comment...


Log in to reply
 

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