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. Custom painted line on QPlainTextEdit doesn't disappear

Custom painted line on QPlainTextEdit doesn't disappear

Scheduled Pinned Locked Moved Solved General and Desktop
qplaintexteditpaintevent
6 Posts 3 Posters 2.6k Views
  • 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.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 14 Mar 2017, 11:57 last edited by
    #1

    Hi guys, back from the dead!

    I have a custom class that inherits from QPlainTextEdit. I overloaded the paintEvent() to draw a vertical line that indicates the 80 characters limit. I do it like this:

    void MyCodeEditor::paintEvent(QPaintEvent* event)
    {
        // Base class
        QPlainTextEdit::paintEvent(event);
    
        // Draw the ruler (vertical line)
        if (_rulerPositionInChars > 0 && _rulerPositionInPixels > 0) {
            QPainter painter(viewport());
            painter.fillRect(_rulerPositionInPixels, 0, RULER_WIDTH, viewport()->height(), RULER_COLOR);
            painter.end();
        }
    }
    

    The _rulerPositionInPixels is an attribute of the MyCodeEditor class which I set to the correct value inside of my MyCodeEditor::setRulerPosition(int numberOfChars) method. At the end of that method I also call update(). Now, depending on the parameter passed to MyCodeEditor::setRulerPosition() it can happen that the line should no longer be drawn at all. I give the user the ability to dynamically toggle the visibility of the line with a QAction inside a QToolbar above the code editor widget.

    The problem I'm experiencing is that when I set the visibility of the line to false which results in the paintEvent() not painting the line, the lines doesn't disappear until there's new text loaded. But even then, only the blocks (in terms of QTextDocument) that were refreshed erase the line so that sometimes there are just fragments / segments left.

    My question: What do I have to do to make the line genuinely disappear? I tried using QWidget::repaint() instead of QWidget::update() but with no luck.
    I thought that the call the the base class QPlainTextEdit::paintEvent() would take care of clearing the widget area and thus removing the preivously drawn line.

    Any ideas?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 14 Mar 2017, 11:58 last edited by
      #2

      Welcome back :)

      1 Reply Last reply
      1
      • ? Offline
        ? Offline
        A Former User
        wrote on 14 Mar 2017, 14:20 last edited by
        #3

        Hi! Just add viewport()->update(); to your paintEvent() override.

        J 1 Reply Last reply 14 Mar 2017, 16:39
        2
        • ? A Former User
          14 Mar 2017, 14:20

          Hi! Just add viewport()->update(); to your paintEvent() override.

          J Offline
          J Offline
          Joel Bodenmann
          wrote on 14 Mar 2017, 16:39 last edited by
          #4

          Oh... well that was stupid...
          Thank you very much, kind Sir. Much appreciated!

          Industrial process automation software: https://simulton.com
          Embedded Graphics & GUI library: https://ugfx.io

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on 14 Mar 2017, 16:45 last edited by
            #5

            :) Btw, you don't have to call painter.end(); it's automatically called when painter gets destroyed.

            1 Reply Last reply
            2
            • J Offline
              J Offline
              Joel Bodenmann
              wrote on 14 Mar 2017, 16:51 last edited by
              #6

              Thanks for the remark, I appreciate it!

              Industrial process automation software: https://simulton.com
              Embedded Graphics & GUI library: https://ugfx.io

              1 Reply Last reply
              1

              1/6

              14 Mar 2017, 11:57

              • Login

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