Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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.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))
    


  • @Shoto
    is bumping allowed


  • Lifetime Qt Champion

    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 ?



  • @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 https://doc.qt.io/qt-5/qsyntaxhighlighter.html#highlightBlock - you get a block of text and can determine the lines and then their lengths.



  • @jsulm
    Aight thanks!


Log in to reply