Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTextEdit setTextColor() problem
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTextEdit setTextColor() problem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 11.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Binary91
    wrote on 20 Oct 2014, 09:29 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IamSumit
      wrote on 20 Oct 2014, 09:59 last edited by
      #2

      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.

      Be Cute

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vovasty
        wrote on 20 Oct 2014, 10:48 last edited by
        #3

        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();
        }
        @

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Binary91
          wrote on 20 Oct 2014, 19:52 last edited by
          #4

          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 :)

          1 Reply Last reply
          0
          • I Offline
            I Offline
            IamSumit
            wrote on 21 Oct 2014, 04:30 last edited by
            #5

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

            Be Cute

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Binary91
              wrote on 21 Oct 2014, 07:13 last edited by
              #6

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

              1 Reply Last reply
              0

              1/6

              20 Oct 2014, 09:29

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved