Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Correctly handling character formats

    General and Desktop
    3
    8
    255
    Loading More Posts
    • 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
      Shoto last edited by

      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 Reply Quote 0
      • S
        Shoto @Shoto last edited by

        @Shoto
        is bumping allowed

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by 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 ?

          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 Reply Quote 2
          • S
            Shoto @SGaist last edited by

            @SGaist

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

            jsulm 1 Reply Last reply Reply Quote 0
            • jsulm
              jsulm Lifetime Qt Champion @Shoto last edited by

              @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 Reply Quote 1
              • S
                Shoto @jsulm last edited by

                @jsulm

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

                jsulm 1 Reply Last reply Reply Quote 0
                • jsulm
                  jsulm Lifetime Qt Champion @Shoto last edited by

                  @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 Reply Quote 4
                  • S
                    Shoto @jsulm last edited by

                    @jsulm
                    Aight thanks!

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post