Force Content Update QPlainTextEdit



  • I am trying to implement a Code editor. And I trying to implement Line Numbers but with the ability to turn on/off line numbers. I took http://qt-project.org/doc/qt-4.8/widgets-codeeditor.html as initial base...

    I added a bool lineNumber and wrapped updateLineNumberArea() and lineNumberAreaPaintEvent() with a if(lineNumber) and altered lineNumberAreaWidth() to return 0 if(lineNumber)

    So I tried to repaint(), update(), resize()... but i did't update the QPlainTextEdit, it did updated only when i typed enter...

    Any idea? I am newbie


  • Moderators

    [quote author="brlebtag" date="1390397731"]
    I added a bool lineNumber and wrapped updateLineNumberArea() and lineNumberAreaPaintEvent() with a if(lineNumber) and altered lineNumberAreaWidth() to return 0 if(lineNumber)[/quote]
    sorry, but this is not understandable.
    Please just post some code of this method.



  • sorry...

    @
    int CodeEditor::lineNumberAreaWidth()
    {
    int digits = 1;
    int max = qMax(1, blockCount());
    while (max >= 10) {
    max /= 10;
    ++digits;
    }
    int space = 10 + fontMetrics().width(QLatin1Char('9')) * digits;

    if(!CodeEditor::line_number)
        space = 0;
    
    return space;
    

    }
    void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
    {
    if(CodeEditor::line_number)
    {
    if (dy)
    lineNumberArea->scroll(0, dy);
    else
    lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());

        if (rect.contains(viewport()->rect()))
            updateLineNumberAreaWidth(0);
    }
    

    }

    void CodeEditor::resizeEvent(QResizeEvent *e)
    {
    if(CodeEditor::line_number)
    {
    QPlainTextEdit::resizeEvent(e);

        QRect cr = contentsRect();
        lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
    }
    

    }

    void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
    {
    if(CodeEditor::line_number)
    {
    QPainter painter(lineNumberArea);
    painter.fillRect(event->rect(), Qt::lightGray);

        QTextBlock block = firstVisibleBlock();
        int blockNumber = block.blockNumber();
        int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
        int bottom = top + (int) blockBoundingRect(block).height();
    
        while (block.isValid() && top <= event->rect().bottom())
        {
            if (block.isVisible() && bottom >= event->rect().top())
            {
                QString number = QString::number(blockNumber + 1);
                painter.setPen(Qt::black);
                painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
                                 Qt::AlignRight, number);
            }
    
            block = block.next();
            top = bottom;
            bottom = top + (int) blockBoundingRect(block).height();
            ++blockNumber;
        }
    }@
    

    I just added a if(CodeEditor::line_number)


Log in to reply
 

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