Important: Please read the Qt Code of Conduct -

Correctly handling character formats

  • 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.setProperty(QTextFormat.FullWidthSelection, True)
                selection.cursor = self.textCursor()
                font = QFont()
                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))

  • @Shoto
    is bumping allowed

  • Lifetime Qt Champion


    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 ?

  • @SGaist

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

  • Lifetime Qt Champion

    @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?

  • @jsulm

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

  • Lifetime Qt Champion

    @Shoto Should be possible with - you get a block of text and can determine the lines and then their lengths.

  • @jsulm
    Aight thanks!

Log in to reply