Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved drawRect in plainTextEdit with qt

    General and Desktop
    2
    8
    995
    Loading More Posts
    • 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
      An other french last edited by An other french

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 3
        • A
          An other french @mrjj last edited by

          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 Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by mrjj

            @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 Reply Quote 2
            • A
              An other french last edited by

              obviously yes
              thanks....

              A 1 Reply Last reply Reply Quote 0
              • A
                An other french @An other french last edited by

                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 Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion last edited by

                  @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 Reply Quote 2
                  • A
                    An other french last edited by

                    for me it s the same block

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post