QPlainTextEdit: how create gaps and highlight text blocks?



  • I am planning create visual diff tool like WInMerge. Two panels, there editors from example "CodeEditor". It Is possible make line numbers with gaps because coder has full control on line numbers. But how highlight many blocks of text? How deny edit on "gaps"? (another question is syntax highlight but it can be later)



  • Highligting, QTextEdit:

        int begin = editor->document()->findBlockByNumber(5).position();
        int end = editor->document()->findBlockByNumber(10).position();
        QTextBlockFormat fmt;
        fmt.setProperty(QTextFormat::FullWidthSelection, true);
        fmt.setBackground(Qt::yellow);
    
        QTextCursor cursor(editor->document());
        cursor.setPosition(begin, QTextCursor::MoveAnchor);
        cursor.setPosition(end, QTextCursor::KeepAnchor);
        cursor.setBlockFormat(fmt);
    


  • One warning: this correct makes one highlighted block, but is bad if I want create many blocks. mergeBlockFormat not helps. Maybe can I use QSyntaxHighlighter for it?

    Is unsolved: MoveAnchor/KeepAnchor not let me multiple blocks, worse - I can't restore default block type.

            QTextCursor cursor(editor->document());
            cursor.setPosition(begin, QTextCursor::MoveAnchor);
            cursor.setPosition(end, QTextCursor::KeepAnchor);
            cursor.setBlockFormat(fmt);
    
            QTextBlockFormat fmtNormal;
            cursor.setPosition(begin, QTextCursor::MoveAnchor);
            cursor.setPosition(end, QTextCursor::KeepAnchor);
            cursor.setBlockFormat(fmtNormal);
    

    This cut me document
    I need:

            QTextBlock block = editor->document()->findBlockByLineNumber(begin);
            block.setBlockFormat(fmtNormal); <---this not  compiles
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.