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. error jumps :( QPainter::begin: Paint device returned engine == 0, type: 1
Forum Updated to NodeBB v4.3 + New Features

error jumps :( QPainter::begin: Paint device returned engine == 0, type: 1

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 287 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.
  • timob256T Offline
    timob256T Offline
    timob256
    wrote on last edited by
    #1

    I am trying to draw with the mouse, but why does the error jump out?

    Here is the whole code:

    mainwindow.cpp

    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    
    void MainWindow::mousePressEvent(QMouseEvent *event)
    {
        QPainter painter(this); // Создаём объект отрисовщика
    
         float rad = 1;
    
         QPointF pt = event->localPos(); // event->Pos() и так и так пробовал
         QPen pen_abris(Qt::black, 2, Qt::SolidLine, Qt::FlatCap); 
    
         painter.setPen(pen_abris);
         painter.drawEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0);
         this->update();
     }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QDebug>
    #include <QPainter>
    #include <QPen>
    #include <QFont>
    #include <QFontMetrics>
    #include <QKeyEvent>
    #include <QRegion>
    #include <QPointF>
    #include <QGraphicsView>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
         MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
         QPoint pos; //координаты точки для рисования
    protected:
    
         void mousePressEvent(QMouseEvent *event);
    };
    #endif // MAINWINDOW_H
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    here is the error

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

    Why doesn't it draw, I can't understand (I set the background)

    jsulmJ 1 Reply Last reply
    0
    • timob256T timob256

      I am trying to draw with the mouse, but why does the error jump out?

      Here is the whole code:

      mainwindow.cpp

      #include "mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
      }
      
      MainWindow::~MainWindow()
      {
      
      }
      
      void MainWindow::mousePressEvent(QMouseEvent *event)
      {
          QPainter painter(this); // Создаём объект отрисовщика
      
           float rad = 1;
      
           QPointF pt = event->localPos(); // event->Pos() и так и так пробовал
           QPen pen_abris(Qt::black, 2, Qt::SolidLine, Qt::FlatCap); 
      
           painter.setPen(pen_abris);
           painter.drawEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0);
           this->update();
       }
      

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QDebug>
      #include <QPainter>
      #include <QPen>
      #include <QFont>
      #include <QFontMetrics>
      #include <QKeyEvent>
      #include <QRegion>
      #include <QPointF>
      #include <QGraphicsView>
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
           MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
           QPoint pos; //координаты точки для рисования
      protected:
      
           void mousePressEvent(QMouseEvent *event);
      };
      #endif // MAINWINDOW_H
      

      main.cpp

      #include "mainwindow.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          return a.exec();
      }
      

      here is the error

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

      Why doesn't it draw, I can't understand (I set the background)

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @timob256 You can only paint inside paintEvent, see https://doc.qt.io/qt-5/qpainter.html
      "Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent().".
      So, move the painting into paintEvent.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3

      • Login

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