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. Correctly handling character formats

Correctly handling character formats

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 613 Views
  • 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.
  • S Offline
    S Offline
    Shoto
    wrote on last edited by
    #1

    So I'm in the progress of creating an IDE with Qt5/PyQt5 and I came across this problem.

    When setting a character format it disappears when the user clicks somewhere else but I need the format to stay (for example: when a line is too long then I have a function to check the line length every time text gets changed and then display a red underline on that line where the line was too long, but after clicking somewhere else the red underline disappears)

    One solution would be to set the extra selections for my QPlainTextEdit every time the user interacts with it but that would mean that when I'm highlighting the current line the user is on then every line that the user has been on will be highlighted.

    Here's a little mock up of how my not so good implementation looks like:

            b = self.textCursor().block()
            if len(b.text()) > 120:
                selection = QTextEdit.ExtraSelection()
                selection.format.setFontUnderline(True)
                selection.format.setUnderlineColor(QColor("#FF0000"))
    
                selection.format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
                selection.format.setProperty(QTextFormat.FullWidthSelection, True)
    
                selection.cursor = self.textCursor()
                selection.cursor.clearSelection()
    
                self.extraSelections_.append(selection)
                self.setExtraSelections(self.extraSelections_)
    
                font = QFont()
                font.setFamily("Iosevka")
                font.setPointSize(10)
    
                QToolTip.setFont(font)
                cursor = self.textCursor()
                current_line = cursor.positionInBlock()
                QToolTip.showText(QCursor.pos(), "Line too long (" + str(current_line) + "> 120) | violation on line: " +
                                  str(b.blockNumber() + 1))
    
    S 1 Reply Last reply
    0
    • S Shoto

      So I'm in the progress of creating an IDE with Qt5/PyQt5 and I came across this problem.

      When setting a character format it disappears when the user clicks somewhere else but I need the format to stay (for example: when a line is too long then I have a function to check the line length every time text gets changed and then display a red underline on that line where the line was too long, but after clicking somewhere else the red underline disappears)

      One solution would be to set the extra selections for my QPlainTextEdit every time the user interacts with it but that would mean that when I'm highlighting the current line the user is on then every line that the user has been on will be highlighted.

      Here's a little mock up of how my not so good implementation looks like:

              b = self.textCursor().block()
              if len(b.text()) > 120:
                  selection = QTextEdit.ExtraSelection()
                  selection.format.setFontUnderline(True)
                  selection.format.setUnderlineColor(QColor("#FF0000"))
      
                  selection.format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
                  selection.format.setProperty(QTextFormat.FullWidthSelection, True)
      
                  selection.cursor = self.textCursor()
                  selection.cursor.clearSelection()
      
                  self.extraSelections_.append(selection)
                  self.setExtraSelections(self.extraSelections_)
      
                  font = QFont()
                  font.setFamily("Iosevka")
                  font.setPointSize(10)
      
                  QToolTip.setFont(font)
                  cursor = self.textCursor()
                  current_line = cursor.positionInBlock()
                  QToolTip.showText(QCursor.pos(), "Line too long (" + str(current_line) + "> 120) | violation on line: " +
                                    str(b.blockNumber() + 1))
      
      S Offline
      S Offline
      Shoto
      wrote on last edited by
      #2

      @Shoto
      is bumping allowed

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #3

        Hi,

        Bumping is allowed but please wait at least 24 hours before doing it. This forum is user driven and not all members of the forum lives in the same time zone as you.

        As for your issue, what about using QSyntaxHighlighter ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          Bumping is allowed but please wait at least 24 hours before doing it. This forum is user driven and not all members of the forum lives in the same time zone as you.

          As for your issue, what about using QSyntaxHighlighter ?

          S Offline
          S Offline
          Shoto
          wrote on last edited by
          #4

          @SGaist

          With QSyntaxHighlighter how would I determine if the line is over 120 characters long?

          jsulmJ 1 Reply Last reply
          0
          • S Shoto

            @SGaist

            With QSyntaxHighlighter how would I determine if the line is over 120 characters long?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Shoto said in Correctly handling character formats:

            how would I determine if the line is over 120 characters long?

            Why do you need that? What do you want to do if the line is shorter and what do you want to do if it is longer? And why exactly 120?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            1
            • jsulmJ jsulm

              @Shoto said in Correctly handling character formats:

              how would I determine if the line is over 120 characters long?

              Why do you need that? What do you want to do if the line is shorter and what do you want to do if it is longer? And why exactly 120?

              S Offline
              S Offline
              Shoto
              wrote on last edited by
              #6

              @jsulm

              Well if it's over 120 then underline the whole line and if not then just keep it normal

              jsulmJ 1 Reply Last reply
              0
              • S Shoto

                @jsulm

                Well if it's over 120 then underline the whole line and if not then just keep it normal

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Shoto Should be possible with https://doc.qt.io/qt-5/qsyntaxhighlighter.html#highlightBlock - you get a block of text and can determine the lines and then their lengths.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply
                4
                • jsulmJ jsulm

                  @Shoto Should be possible with https://doc.qt.io/qt-5/qsyntaxhighlighter.html#highlightBlock - you get a block of text and can determine the lines and then their lengths.

                  S Offline
                  S Offline
                  Shoto
                  wrote on last edited by
                  #8

                  @jsulm
                  Aight thanks!

                  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