error jumps :( QPainter::begin: Paint device returned engine == 0, type: 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)
-
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)
@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.