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 highlight selected text in QTextEdit?
Qt 6.11 is out! See what's new in the release blog

how to highlight selected text in QTextEdit?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.4k 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.
  • chaochaoC Offline
    chaochaoC Offline
    chaochao
    wrote on last edited by
    #1

    I has realised a small app like NoteBook.Now,I want to hight selected text,but I have no idea about this. I create a class that inheit QSyntaxHighlighter.

    //your code here
    .h
    class Myhighlitghter : public QSyntaxHighlighter
    {
    public:
        explicit Myhighlitghter(QTextDocument *parent=0);
    protected:
         virtual void highlightBlock(const QString &text);
    };
    .cpp
    Myhighlitghter::Myhighlitghter(QTextDocument *parent)
        :QSyntaxHighlighter(parent)
    {
    
    }
    
    void Myhighlitghter::highlightBlock(const QString &text)
    {
        QTextCharFormat charformat;
        charformat.setFontWeight(QFont::Bold);
        charformat.setForeground(Qt::red);
    
        QString pattern="\\bchar\\b";
    
        QRegExp exp(pattern);
    
        int index = text.indexOf(exp);
    
        while(index>=0)
        {
            int len=exp.matchedLength();
    
            setFormat(index,len,charformat);
    
            index = text.indexOf(exp,index+len);
        }
    }
    

    then I create a mainwindow that has Qtextedit and Myhighlitghter object.Now , I want to select text in textedit and hightlight them.

    thank for your help

    Ni.SumiN 1 Reply Last reply
    0
    • chaochaoC chaochao

      I has realised a small app like NoteBook.Now,I want to hight selected text,but I have no idea about this. I create a class that inheit QSyntaxHighlighter.

      //your code here
      .h
      class Myhighlitghter : public QSyntaxHighlighter
      {
      public:
          explicit Myhighlitghter(QTextDocument *parent=0);
      protected:
           virtual void highlightBlock(const QString &text);
      };
      .cpp
      Myhighlitghter::Myhighlitghter(QTextDocument *parent)
          :QSyntaxHighlighter(parent)
      {
      
      }
      
      void Myhighlitghter::highlightBlock(const QString &text)
      {
          QTextCharFormat charformat;
          charformat.setFontWeight(QFont::Bold);
          charformat.setForeground(Qt::red);
      
          QString pattern="\\bchar\\b";
      
          QRegExp exp(pattern);
      
          int index = text.indexOf(exp);
      
          while(index>=0)
          {
              int len=exp.matchedLength();
      
              setFormat(index,len,charformat);
      
              index = text.indexOf(exp,index+len);
          }
      }
      

      then I create a mainwindow that has Qtextedit and Myhighlitghter object.Now , I want to select text in textedit and hightlight them.

      thank for your help

      Ni.SumiN Offline
      Ni.SumiN Offline
      Ni.Sumi
      wrote on last edited by
      #2

      @chaochao

      QTextCursor will help you to select the text, delete , replace, background color of the selected text.

      chaochaoC 1 Reply Last reply
      1
      • Ni.SumiN Ni.Sumi

        @chaochao

        QTextCursor will help you to select the text, delete , replace, background color of the selected text.

        chaochaoC Offline
        chaochaoC Offline
        chaochao
        wrote on last edited by
        #3

        @Ni.Sumi very thanks, I will have a try

        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