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. I have change the color of each line text in QTextWidget one by one
Forum Updated to NodeBB v4.3 + New Features

I have change the color of each line text in QTextWidget one by one

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 4.2k Views 2 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    I have change the color of each line text in QTextWidget one by one. I have multiple lines QtextEdit .

    Could you please some one suggest how to do that

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Did you use HTML or just plaintext ?
      https://stackoverflow.com/questions/2857864/qtextedit-with-different-text-colors-qt-c

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by Qt Enthusiast
        #3

        I have lines

        line1
        line2
        line3
        intially lines are not edited

        now I have C++ function

        colorEach:Line() {
        getEachline from QtexLine and
        change the color of ltext in QtextEdit

        }

        J.HilkJ 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4
          QTextCursor cursor(textEdit->document());
          cursor.movePosition(QTextCursor::Start);
          while(cursor.movePosition(QTextCursor::EndOfLine,QTextCursor::MoveAnchor)){
          QTextCharFormat tempFormat = cursor.charFormat();
          tempFormat.setBackground(Qt::yellow);
          cursor.setCharFormat(tempFormat);
          cursor.movePosition(QTextCursor::NoMove); //this might need to be cursor.movePosition(QTextCursor::NextCharacter) unsure
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3
          • Q Qt Enthusiast

            I have lines

            line1
            line2
            line3
            intially lines are not edited

            now I have C++ function

            colorEach:Line() {
            getEachline from QtexLine and
            change the color of ltext in QtextEdit

            }

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @Qt-Enthusiast
            an alternative to @VRonin example

            void changeColors(QTextEdit *edit){
                QString newText;
            
                QVector<QString> colors {
                    "<font color=\"red\">",
                    "<font color=\"green\">",
                    "<font color=\"blue\">"
                };
            
                QString endFont = "</font>";
                QString ParagraphEnd = "</p>";
            
                QString text = edit->toHtml();
                auto ref = text.split(ParagraphEnd);
            
                for(int i(0); i < ref.size();i++){
                    QString s = ref.at(i);
                    int insert = s.lastIndexOf(">");
                    s.insert(insert+1,colors.at(i%3));
                    s.append(endFont+ParagraphEnd);
                    newText.append(s);
                }
                edit->setHtml(newText);
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0

            • Login

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