Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Force Content Update QPlainTextEdit
Qt 6.11 is out! See what's new in the release blog

Force Content Update QPlainTextEdit

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brlebtag
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      [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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • B Offline
        B Offline
        brlebtag
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved