简体   繁体   English

QPainter :: begin在调试模式下使程序崩溃

[英]QPainter::begin crashes the program in debug mode

1. Problem description 1.问题描述

Calling QPainter::begin works fine when the program is run normally, but causes it to crash when executed in Debug mode. 当程序正常运行时,调用QPainter :: begin可以正常工作,但是在调试模式下执行时,导致崩溃。 Any ideas what is the reason for that? 任何想法是什么原因?


2. Environment 2.环境

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

3. Example code 3.示例代码

MainWindow.h MainWindow.h

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
};

MainWindow.cpp 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 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 4.调试器的输出

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

reposting from bugreports.qt.io/browse/QTBUG-64581 从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. 如果您开始使用QPainter :: begin()进行绘制,则可以期望在完成绘制之前已通过QPaintDevice并且QPaintEngine仍处于活动状态。 You should not destroy QPaintEngine while painting is active (end() is not called). 绘画处于活动状态(不调用end())时,请勿破坏QPaintEngine。 In the example QPixmap is destroyed before QPainter has finished painting. 在示例中,QPixmap在QPainter完成绘制之前已销毁。 Needs to call end() before or make sure that the pixmap is alive. 需要先调用end()或确保像素图还处于活动状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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