简体   繁体   中英

QPainter::begin crashes the program in debug mode

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

  • Windows 7 Pro 64 bit
  • Qt 5.9.2
  • MSVC 2017
  • Windows Kits\\10\\Debuggers\\x64\\cdb.exe

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

在此处输入图片说明 在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM