Reputation: 8399
1. Problem description
Calling QPainter::begin works fine when the program is run normally, but causes it to crash when executed in Debug mode. Any ideas what is the reason for that?
2. Environment
3. Example code
MainWindow.h
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
};
MainWindow.cpp
#include "MainWindow.h"
#include "Painter.h"
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
auto *label = new QLabel(this);
label->setPixmap(Painter().paint());
setCentralWidget(label);
}
Painter.h
#include <QObject>
class Painter : public QObject
{
Q_OBJECT
public:
explicit Painter(QObject *parent = nullptr);
QPixmap paint();
};
Painter.cpp
#include "Painter.h"
#include <QPainter>
Painter::Painter(QObject *parent) : QObject(parent)
{
}
QPixmap Painter::paint()
{
QPainter painter;
QPixmap pixmap(16, 16);
pixmap.fill(Qt::transparent);
painter.begin(&pixmap); // <-- program crashes here on Debug
return pixmap;
}
4. Debugger's output
Upvotes: 0
Views: 2960
Reputation: 346
reposting from bugreports.qt.io/browse/QTBUG-64581
If you begin painting by QPainter::begin() you would expect to have passed QPaintDevice and QPaintEngine is alive before painting is finished. You should not destroy QPaintEngine while painting is active (end() is not called). In the example QPixmap is destroyed before QPainter has finished painting. Needs to call end() before or make sure that the pixmap is alive.
Upvotes: 3