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. How to change font weight at various locations within a QTextEdit
Forum Updated to NodeBB v4.3 + New Features

How to change font weight at various locations within a QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 2 Posters 10.9k 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.
  • M Offline
    M Offline
    mulfycrowh
    wrote on last edited by
    #6

    What do you think about the following thread :

    http://www.qtcentre.org/threads/24331-How-to-set-text-and-font-in-qTextEdit

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

      well it shows a slightly smarter way to use html and
      a cursor. so yes could also be used even u are then very close to the normal textformat way.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mulfycrowh
        wrote on last edited by
        #8

        Here is what I wrote:

        // convert a number into a QString made of 2 characters
        QString Tools::toHexString(int value)
        {
        	QString sHex = QString::number(value, 16).toUpper();
        	if (sHex.length() == 1)
        		sHex.prepend('0');
        	return sHex;
        }
        
        
        // highlight the text in QTextEdit edit at location for charcount characters
        // (red, green, blue) is the color
        // fontsize is the size of the font
        // string may be highlighted with bold and underlined styles
        void Tools::toHtml(QTextEdit* edit, int location, int charcount, int red, int green, int blue, int fontsize, bool bold, bool underlined)
        {
        	QTextCursor cursor(edit->document());
        	QString highlight;
        	QString sfont_beg = "<FONT ";
        	QString sfont_end = "</FONT>";
        	QString scolor = "color=#" + toHexString(red) + toHexString(green) + toHexString(blue) + " ";
        	QString sbold = "span style=font-weight:bold ";
        	QString sunderlined = "span style=text-decoration:underline ";
        	QString ssize = "size=" + QString::number(fontsize) + ">";
        	QString sHtml;
        
        	cursor.setPosition(location, QTextCursor::MoveAnchor);
        	cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, charcount);
        	highlight = cursor.selectedText();
        	sHtml = sfont_beg + scolor.toUpper();
        	if (bold)
        		sHtml = sHtml.append(sbold);
        	if (underlined)
        		sHtml = sHtml.append(sunderlined);
        	sHtml = sHtml + ssize + highlight + sfont_end;
        	cursor.insertHtml(sHtml);
        
        }
        

        It runs pretty well.
        The trouble comes when I add several times (title + text) to the QTextEdit.
        Each title has a length of 26 characters.
        The length of each text is different.
        I computed the location for each title as follows:

        0 for the first one
        26 + text0.length()
        26 + text0.length() + 26 + text1.length()
        ...

        If I apply this algorithm when I have appended all the texts to the QTextEdit, the fist title is well highlighted but there is a shift for the following ones.
        I don't understand why.

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

          hi
          nothing pop out in the code but i wonder if all (test) Titles are same color.
          as you say
          if (sHex.length() == 1)
          sHex.prepend('0');

          so maybe other color can alter the 26 offset ?

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mulfycrowh
            wrote on last edited by
            #10

            Yep. All titles are the same color.
            Here is the code to highlight the titles:

            	for (int Index = 0; Index < m_process.size(); ++Index)
            		m_tools->toHtml(ui.Command_Edit, referenceLocation[Index], 17 + msg_description.length(), 0, 133, 255, 25, false, false);
            

            But the trouble is that I encounter a shift about the highlighting. The first title is OK (obviously because 0), the following highlightings are false, I mean the location.
            I don't see any problem.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mulfycrowh
              wrote on last edited by
              #11

              I think I found the trouble : we have to substract the count of CR (or LF).
              But I have to add 2, I don't understand why. It would mean that there are 2 extra characters that are not text.
              So, for the algorithm of computing the location (vector referenceLocation - first element equals to 0), if I write:

              			if (Index != 0) {
              				referenceLocation.append(referenceLocation[Index - 1]);
              				referenceLocation[Index] += m_command->getCommandFileContent(UNCOMPRESSED, UNCODED).size();
              				referenceLocation[Index] += 17 + msg_description.length();
              				referenceLocation[Index] -= m_command->getCommandFileContent(UNCOMPRESSED, UNCODED).count('\n');
              				referenceLocation[Index] += 2;
              

              information : title is made of 17 characters and description,

              it runs perfectly !

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

                good found.
                well a newline is \n\r on windows.
                so maybe thats why with 2.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mulfycrowh
                  wrote on last edited by
                  #13

                  Hi folks !

                  I open this topic again because I get a weird thing.
                  Here is what I wrote:

                  void Tools::toHtml(QPlainTextEdit* edit, int location, int charcount, int red, int green, int blue, int fontsize, bool bold, bool underlined)
                  {
                  	QTextCursor cursor(edit->document());
                  	QString highlight;
                  	QString sfont_beg = "<FONT ";
                  	QString sfont_end = "</FONT>";
                  	QString scolor = "color=#" + toHexString(red) + toHexString(green) + toHexString(blue) + " ";
                  	QString sbold = "span style=font-weight:bold ";
                  	QString sunderlined = "span style=text-decoration:underline ";
                  	QString ssize = "size=" + QString::number(fontsize) + ">";
                  	QString sHtml;
                  
                  	cursor.setPosition(location, QTextCursor::MoveAnchor);
                  	cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, charcount);
                  	highlight = cursor.selectedText();
                  	sHtml = sfont_beg + scolor.toUpper();
                  	if (bold)
                  		sHtml = sHtml.append(sbold);
                  	if (underlined)
                  		sHtml = sHtml.append(sunderlined);
                  	sHtml = sHtml + ssize + highlight + sfont_end;
                  	cursor.insertHtml(sHtml);
                  
                  }
                  

                  I call the function here:

                  m_tools->toHtml(m_descriptionPlainTextEdit, 0, 17 + msg_description.length(), 0, 133, 255, 25, false, false);
                  

                  The title that has to be highlighted in blue has a length equal to 17 + msg_description.length().
                  Everything perfectly runs the first time, not the following ones : all the text in the text edit is highlighted in blue.
                  I don't see any trouble.

                  PS: the text edit is cleared each time.

                  Many thanks for help !

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

                    Hi
                    ïf you put edit->clear();
                    in top, it really should do the same each time.

                    M 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      Hi
                      ïf you put edit->clear();
                      in top, it really should do the same each time.

                      M Offline
                      M Offline
                      mulfycrowh
                      wrote on last edited by
                      #15

                      @mrjj Hi ! What do you mean in top ? In toHtml ? It would delete all the content.

                      mrjjM 1 Reply Last reply
                      0
                      • M mulfycrowh

                        @mrjj Hi ! What do you mean in top ? In toHtml ? It would delete all the content.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #16

                        @mulfycrowh
                        Well you said
                        "PS: the text edit is cleared each time."
                        So I assumed you meant pr call to the function.

                        If all becomes blue it sounds like all is one paragraph.

                        You might need to use <p > </p> if the end result is something like

                        M 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @mulfycrowh
                          Well you said
                          "PS: the text edit is cleared each time."
                          So I assumed you meant pr call to the function.

                          If all becomes blue it sounds like all is one paragraph.

                          You might need to use <p > </p> if the end result is something like

                          M Offline
                          M Offline
                          mulfycrowh
                          wrote on last edited by
                          #17

                          @mrjj The idea sounds interesting. I'll test it.
                          But why everything OK the first time and all the text blue with big size font the other ones ?
                          It acts as the QTextEdit keeps backup of something.
                          Thanks again !

                          mrjjM 1 Reply Last reply
                          0
                          • M mulfycrowh

                            @mrjj The idea sounds interesting. I'll test it.
                            But why everything OK the first time and all the text blue with big size font the other ones ?
                            It acts as the QTextEdit keeps backup of something.
                            Thanks again !

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #18

                            @mulfycrowh

                            • But why everything OK the first time and all the text blue with big size font the other ones ?

                            I have not check output of the code but I assume it becomes
                            one big paragraph and therefore it is all blue.

                            But only way to tell is to see the generated HTML. step by step.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mulfycrowh
                              wrote on last edited by
                              #19

                              I added the following lines to toHtml ant it runs !

                              cursor.setPosition(charcount, QTextCursor::MoveAnchor);
                              cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, edit->document()->characterCount());
                              highlight = cursor.selectedText();
                              sHtml = "<FONT face=\"Arial\" font-weight=\"normal\" COLOR=#000000 size=\"3\">" + highlight + "</FONT>";
                              cursor.insertHtml(sHtml);
                              
                              1 Reply Last reply
                              1
                              • M Offline
                                M Offline
                                mulfycrowh
                                wrote on last edited by
                                #20

                                Nevertheless, I do not understand size=3 because, in the constructor I wrote:

                                	font.setPointSize(10);
                                

                                Why 3 instead 10 ?

                                3 gives the same size.

                                mrjjM 1 Reply Last reply
                                0
                                • M mulfycrowh

                                  Nevertheless, I do not understand size=3 because, in the constructor I wrote:

                                  	font.setPointSize(10);
                                  

                                  Why 3 instead 10 ?

                                  3 gives the same size.

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #21

                                  @mulfycrowh
                                  Not sure what unit "size" is in. Could be an index to a list.
                                  so its size "3".

                                  M 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @mulfycrowh
                                    Not sure what unit "size" is in. Could be an index to a list.
                                    so its size "3".

                                    M Offline
                                    M Offline
                                    mulfycrowh
                                    wrote on last edited by
                                    #22

                                    @mrjj Thank you for everything !

                                    1 Reply Last reply
                                    1

                                    • Login

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