How to change current line format in QTextEdit without selection?

后端 未结 1 1965
醉话见心
醉话见心 2021-01-20 16:29

there! I want to find out how to change current line format in QTextEdit?

In the document I read that

\"Formatting can be applied to the cu

1条回答
  •  隐瞒了意图╮
    2021-01-20 16:51

    Pls, check if an example below would work for you, it should change the current text block format and font.

    QTextCursor cursor(myTextEdit->textCursor());
    
    // change block format (will set the yellow background)
    QTextBlockFormat blockFormat = cursor.blockFormat();
    blockFormat.setBackground(QColor("yellow"));
    blockFormat.setNonBreakableLines(true);
    blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
    cursor.setBlockFormat(blockFormat);
    
    // change font for current block's fragments
    for (QTextBlock::iterator it = cursor.block().begin(); !(it.atEnd()); ++it)
    {
        QTextCharFormat charFormat = it.fragment().charFormat();
        charFormat.setFont(QFont("Times", 15, QFont::Bold));
    
        QTextCursor tempCursor = cursor;
        tempCursor.setPosition(it.fragment().position());
        tempCursor.setPosition(it.fragment().position() + it.fragment().length(), QTextCursor::KeepAnchor);
        tempCursor.setCharFormat(charFormat);
    }
    

    hope this helps, regards

    0 讨论(0)
提交回复
热议问题