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. TextEdit painting C++
Forum Updated to NodeBB v4.3 + New Features

TextEdit painting C++

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.0k 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.
  • L Offline
    L Offline
    LittleFire
    wrote on last edited by
    #1

    Hi everyone,
    I read many posts of how to paint on Qtextedit, but I cannot get out of the problem.

    I create a form with a qtextedit named text and a class named textedit. Then, I wrote paintEvent(QPaintEvent* event) method in this way:

    void textedit::paintEvent(QPaintEvent* event)
    {
            QCursor cursorQ = ui->text->cursor();
            QPoint point= cursorQ.pos();
            QPainter p(this);
            p.drawText(QPoint(30,30), "|");
            p.drawText(point, "|");
    }
    

    The first drawText works and draw "|" character out of the QTextEdit area in the QPoint(30, 30), the second don't and I don't understand why.
    It's produced:
    "QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1"
    output.
    Maybe it is because QPainter is reffered to the entire window and QPoint only to Qtextedit area? If this is the reason, how can I get what I want?
    I read some posts where people recommend using

    QPainter p(viewport());
    

    but it produced "used of undeclared identifier viewport " error.

    What can I have miss?

    (I used drawtext only as example for me, for understanding how to paint in the QTextEdit area)

    There .cpp and .h files:

    #ifndef TEXTEDIT_H
    #define TEXTEDIT_H
    
    #include <QMainWindow>
    #include <QCursor>
    #include <unistd.h>
    #include <QTextCharFormat>
    #include <QTextCursor>
    #include <QDebug>
    #include <QPainter>
    
    namespace Ui {
    class textedit;
    }
    
    class textedit : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit textedit(QWidget *parent = nullptr);
        ~textedit();
    
    private:
        Ui::textedit *ui;
        QTextCursor cursor;
    
    protected:
        void paintEvent(QPaintEvent* event);
    };
    
    #endif // TEXTEDIT_H
    
    #include "textedit.h"
    #include "ui_textedit.h"
    
    textedit::textedit(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::textedit)
    {
        ui->setupUi(this);
        ui->text->setText("Hello, World!\nAnotherLine.\n");
    
    }
    
    textedit::~textedit()
    {
        delete ui;
    }
    void textedit::paintEvent(QPaintEvent* event)
    {
            QCursor cursorQ = ui->text->cursor();
            QPoint point= cursorQ.pos();
            QPainter p(this);
            p.drawText(QPoint(30,30), "|");
            p.drawText(point, "|");
    }
    

    Thanks for any reply or advice.

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

      Hi,

      point is likely local to the QTextEdit so completely unrelated to your QMainWindow. You have to translate the coordinate to make it draw where you want.

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

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        point is likely local to the QTextEdit so completely unrelated to your QMainWindow. You have to translate the coordinate to make it draw where you want.

        L Offline
        L Offline
        LittleFire
        wrote on last edited by
        #3

        @SGaist how can I do this? For what I need I have to start from the cursor position

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

          Take a look at the various point mapping related functions.

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

          L 1 Reply Last reply
          1
          • SGaistS SGaist

            Take a look at the various point mapping related functions.

            L Offline
            L Offline
            LittleFire
            wrote on last edited by
            #5

            @SGaist Thank you!

            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