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. Unable to use drawContents inside a QTextEdit

Unable to use drawContents inside a QTextEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 878 Views 2 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.
  • A Offline
    A Offline
    anphetamina
    wrote on last edited by
    #1

    I have a class that include a reference to a QTextEdit and a QPainter object initialized as following:

    class MyClass : QObject {
    
    QWidget &parent;
    Ui::MainWindow &ui;
    QPaint painter;
    ...
    }
    
    MyClass::MyClass() : painter(ui.textEdit) {
    
    connect(ui.textEdit, &QTextEdit::cursorPositionChanged, this, &MyClass::cursorPositionChanged);
    painter.setPen(Qt::yellow);
    
    }
    
    

    And this is the slot connected to the signal:

    void MyClass::cursorPositionChanged() {
        QRect cursorRect = ui.textEdit->cursorRect();
        painter.drawRect(cursorRect);
        ui.textEdit->document()->drawContents(&painter);
    }
    

    Once I move the cursor inside the QTextEdit document the output is:

    QPainter::drawRects: Painter not active
    QPainter::save: Painter not active
    QPainter::pen: Painter not active
    QPainter::setPen: Painter not active
    QPainter::pen: Painter not active
    QPainter::setPen: Painter not active
    QPainter::pen: Painter not active
    QPainter::setPen: Painter not active
    QPainter::setPen: Painter not active
    QPainter::setPen: Painter not active
    QPainter::restore: Unbalanced save/restore
    

    and it doesn't paint anything in the document. What I'm missing?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can't paint on a widget in an arbitrary manner like you do. And you can't paint on a widget from an arbitrary source either.

      What exactly do you want to achieve ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        anphetamina
        wrote on last edited by
        #3

        I need to paint the current cursor in the document

        SGaistS 1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          [EDITED]
          A QPainter must be begin()ed / constructed with a QPaintDevice to use.
          And if the device is a widget, then it need to be begin()ed / constructed and used only in its paintEvent.
          So if drawContents is not called in a widget's paintEvent, it is usually used to draw the document to another QPaintDevice, like an image or a pdf.
          Here I even don't know what do you want to paint on since you never set any QPaintDevice to the painter.

          1 Reply Last reply
          1
          • A anphetamina

            I need to paint the current cursor in the document

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @anphetamina said in Unable to use drawContents inside a QTextEdit:

            I need to paint the current cursor in the document

            QTextEdit already draws the cursor where it is so that's a bit strange. Can you give more details ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 2 Replies Last reply
            1
            • SGaistS SGaist

              @anphetamina said in Unable to use drawContents inside a QTextEdit:

              I need to paint the current cursor in the document

              QTextEdit already draws the cursor where it is so that's a bit strange. Can you give more details ?

              A Offline
              A Offline
              anphetamina
              wrote on last edited by
              #6

              @SGaist I need to paint a line on the document cursor using a specified color

              1 Reply Last reply
              0
              • SGaistS SGaist

                @anphetamina said in Unable to use drawContents inside a QTextEdit:

                I need to paint the current cursor in the document

                QTextEdit already draws the cursor where it is so that's a bit strange. Can you give more details ?

                A Offline
                A Offline
                anphetamina
                wrote on last edited by
                #7

                @SGaist overriding paintEvent from QTextEdit I successfully painted a vertical line in the cursor position but I need to draw more lines on demand, is that possible?

                Christian EhrlicherC 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  What do you mean by on demands ?
                  What kind of line ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • A anphetamina

                    @SGaist overriding paintEvent from QTextEdit I successfully painted a vertical line in the cursor position but I need to draw more lines on demand, is that possible?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @anphetamina said in Unable to use drawContents inside a QTextEdit:

                    but I need to draw more lines on demand, is that possible?

                    Then draw more lines in paintEvent() ...

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    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