Кастомные подчеркивания в PyQt5 QTextCharFormat

Есть такой код:

class QBoard(QTextEdit):
    def __init__(self):
        super(QBoard, self).__init__()

    def makeBold(self):
        format = QTextCharFormat()
        format.setFontWeight(QFont.Bold)

        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

    def makeItalic(self):
        format = QTextCharFormat()
        format.setFontItalic(1)

        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

    def makeTitle(self):
        format = QTextCharFormat()

        font = QFont()
        font.setPixelSize(30)
        font.setWeight(QFont.Bold)

        format.setFont(font)

        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

    def makeCode(self):
        format = QTextCharFormat()
        format.setFontStyleHint(QFont.TypeWriter)
        format.setFont(QFont('Courier'))

        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

    def makeNormal(self):
        format = QTextCharFormat()
        font = QFont()
        font.setPixelSize(20)
        format.setFont(font)

        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

    def makeFG(self):
        color = QColorDialog().getColor()
        print(color)

        format = QTextCharFormat()
        try:
            format.setForeground(QColor(color))

            cursor = self.textCursor()
            cursor.mergeCharFormat(format)
        except Exception as e:
            print(e)

    def makeBG(self):
        color = QColorDialog().getColor()
        print(color)

        format = QTextCharFormat()
        try:
            format.setBackground(QColor(color))

            cursor = self.textCursor()
            cursor.mergeCharFormat(format)
        except Exception as e:
            print(e)

    def makeUnderLine(self):

        format = QTextCharFormat()
        format.setUnderlineStyle(
            QTextCharFormat.SingleUnderline)
        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

    def makeSub(self):
        format = QTextCharFormat()
        format.setFont(QFont())
        cursor = self.textCursor()
        cursor.mergeCharFormat(format)
    
    def makeDash(self):
        format = QTextCharFormat()
        format.setUnderlineStyle(
            QTextCharFormat.DashUnderline)
        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

    def makeWrong(self):
        format = QTextCharFormat()
        format.setFontStrikeOut(1)
        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

    def makeWave(self):
        format = QTextCharFormat()
        format.setUnderlineStyle(
            QTextCharFormat.WaveUnderline)
        cursor = self.textCursor()
        cursor.mergeCharFormat(format)

Функция makeBold делает выделенный текст болдом, makeBG добавляет выделенному тексту фон и т.д.

Теперь вопрос(ы):

  • Как сделать двойное подчеркивание?
  • Как сделать подчеркивание аля:
-·-·-·-

Ума не приложу, в QTextCharFormat нет таких объектов. (


Ответы (2 шт):

Автор решения: eri
text-decoration-style: solid|double|dotted|dashed|wavy|initial|inherit;

Двайное подчеркивание можно попробровать применив css. QTextEdit поддерживает базовые стили и форматирование html.

а совсем кaстомное проще нарисовать поверх.

→ Ссылка
Автор решения: S. Nick

enum QTextCharFormat::UnderlineStyle - описывает различные способы рисования подчеркнутого текста. https://doc.qt.io/qt-5/qtextcharformat.html#UnderlineStyle-enum

я не проверял ваш пример, потому что он не воспроизводимый, но я думаю, что вам достаточно установить

format.setUnderlineStyle(QTextCharFormat.DashDotLine)

и вы получите желаемое _ . _ . _ . _

`

def makeUnderLine(self):

    format = QTextCharFormat()
    format.setUnderlineStyle(
    #    QTextCharFormat.SingleUnderline)
        QTextCharFormat.DashDotLine)                  # <<<-----<
    cursor = self.textCursor()
    cursor.mergeCharFormat(format)

введите сюда описание изображения


А кстати где можно посмотреть вот этот прикольный пример, который у вас на картинке?

README

PyQt provides powerful document-oriented rich text engine that supports Unicode and right-to-left scripts. Documents can be manipulated using a cursor-based API, and their contents can be imported and exported as both HTML and in a custom XML format.

Text is rendered using anti-aliased outline fonts to provide the best possible on-screen representation.

The example launcher provided with PyQt can be used to explore each of the examples in this directory.

Documentation for these examples can be found via the Tutorial and Examples link in the main Qt documentation.

Finding the PyQt Examples and Demos launcher

On Windows:

The launcher can be accessed via the Windows Start menu. Select the menu entry entitled "Examples and Demos" entry in the submenu containing PyQt5.

On all platforms:

The source code for the launcher can be found in the examples/demos/qtdemo directory in the PyQt package.

→ Ссылка