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. drawRect in plainTextEdit with qt
Forum Updated to NodeBB v4.3 + New Features

drawRect in plainTextEdit with qt

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.6k 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.
  • A Offline
    A Offline
    An other french
    wrote on 15 Apr 2018, 11:26 last edited by An other french
    #1

    Hello,
    I try to highlight some lines in a plaintextEdit (example)
    First, i need to show a Rect in the Text edit.
    I add a button to draw a rect

    void form2::on_pushButton_5_clicked()
    {
            QPainter painter(ui->plainTextEdit); //With this, it is the same pb
            painter.drawRect(0,0,100,100);
    }
    

    The debugger said :

    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::drawRects: Painter not active
    

    Some helps Please.
    Thanks for reading.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 15 Apr 2018, 11:34 last edited by
      #2

      Hi
      you cannot draw with painter outside of PainEvent.
      Also you can override plainTextEdit PainEvent and draw a rect but it wont follow
      text like an inserted image at all.
      If you want to color the words, this can work
      http://www.qtcentre.org/threads/53567-QPlainTextEdit-highlight-word
      but you wont be able to draw rects.

      A 1 Reply Last reply 15 Apr 2018, 11:56
      3
      • M mrjj
        15 Apr 2018, 11:34

        Hi
        you cannot draw with painter outside of PainEvent.
        Also you can override plainTextEdit PainEvent and draw a rect but it wont follow
        text like an inserted image at all.
        If you want to color the words, this can work
        http://www.qtcentre.org/threads/53567-QPlainTextEdit-highlight-word
        but you wont be able to draw rects.

        A Offline
        A Offline
        An other french
        wrote on 15 Apr 2018, 11:56 last edited by
        #3

        I highlight the first line of the text to see what happen

        ui->plainTextEdit->setFocus();
            QTextCursor tmpCursor =ui->plainTextEdit->textCursor();
            tmpCursor.movePosition(QTextCursor::Start);
            tmpCursor.select(QTextCursor::LineUnderCursor);
            ui->plainTextEdit->setTextCursor(tmpCursor);
        

        After i find "Code Editor Example"
        The construction af the window finish by the code "highlightCurrentLine();"

        void CodeEditor::highlightCurrentLine()
        {
            QList<QTextEdit::ExtraSelection> extraSelections;
        
            if (!isReadOnly()) {
                QTextEdit::ExtraSelection selection;
        
                QColor lineColor = QColor(Qt::yellow).lighter(160);
        
                selection.format.setBackground(lineColor);
                selection.format.setProperty(QTextFormat::FullWidthSelection, true);
                selection.cursor = textCursor();
                selection.cursor.clearSelection();
                extraSelections.append(selection);
            }
        
            setExtraSelections(extraSelections);
        }
        

        no way to include textCursor(); and !isReadOnly() in the scope ....
        the incude seem correct...

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 15 Apr 2018, 12:45 last edited by mrjj
          #4

          @An-other-french said in drawRect in plainTextEdit with qt:

          no way to include textCursor(); and !isReadOnly() in the scope ....

          What you mean ?
          If CodeEditor is not a subclass of QPlainText then you need something like
          ui->plainTextEdit->textCursor();
          or how ever it has access to the plainTextEdit.

          1 Reply Last reply
          2
          • A Offline
            A Offline
            An other french
            wrote on 15 Apr 2018, 14:23 last edited by
            #5

            obviously yes
            thanks....

            A 1 Reply Last reply 15 Apr 2018, 15:19
            0
            • A An other french
              15 Apr 2018, 14:23

              obviously yes
              thanks....

              A Offline
              A Offline
              An other french
              wrote on 15 Apr 2018, 15:19 last edited by
              #6

              As we saw in the Qt"s exemple, the highlightCurrentLine function don't highlight the line if it was written in two lines...
              FindBlockByLineNumber gave a different highlightCurrentLine.

              one way is to forbidden the carriage return.
              other is to test if the line carry on or not ...

              An idea?!!

              The purpose is to highlight all lines with the same criterias.
              For example, highlight all lines which contains the word John.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 15 Apr 2018, 15:52 last edited by
                #7

                @An-other-french said in drawRect in plainTextEdit with qt:

                FindBlockByLineNumber

                Hi, i think it will be another block if written like 2 lines. Just like a paragraph in a
                word processor. so i think you have to check if it continues. However, if you can
                disable CR that would help.

                1 Reply Last reply
                2
                • A Offline
                  A Offline
                  An other french
                  wrote on 16 Apr 2018, 01:34 last edited by
                  #8

                  for me it s the same block

                  1 Reply Last reply
                  0

                  1/8

                  15 Apr 2018, 11:26

                  • Login

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