【问题标题】:QPainter::begin crashes the program in debug modeQPainter::begin 在调试模式下使程序崩溃
【发布时间】:2017-11-16 21:27:40
【问题描述】:

1.问题描述

在程序正常运行时调用QPainter::begin 可以正常工作,但在Debug 模式下执行时会导致程序崩溃。任何想法是什么原因?


2。环境

  • Windows 7 专业版 64 位
  • Qt 5.9.2
  • MSVC 2017
  • Windows 工具包\10\Debuggers\x64\cdb.exe

3.示例代码

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

【问题讨论】:

  • 绘制函数中的 QPainter 变量是否不需要以某种方式初始化?
  • @bennji_of_the_overflow 怎么样?
  • 我在Linux上用Qt 5.9.2试过了,效果很好,可能是windows才有的bug,建议你报告一下。
  • @eyllanesc,我怀疑调试器会导致问题,因为否则它 100% 的时间都可以正常工作。事实上,自从我将 Qt 升级到 5.9.1 和 5.9.2 之后,我就遇到了调试问题。在某些时候,不仅应用程序,而且 Qt Creator 在调试时也会崩溃。虽然我不知道我是否可以用一个最小且可验证的例子来重现那个确切的例子。无论如何,我想在向 Qt 报告之前进一步调查。今天我将在 Win 10 下做同样的练习,看看会发生什么。当然,我会分享结果。
  • @eyllanesc,我确认了 Windows 10 上的问题。我想我现在有足够的证据来报告它。

标签: c++ windows debugging qt5 qpainter


【解决方案1】:

从 bugreports.qt.io/browse/QTBUG-64581 转帖

如果您通过 QPainter::begin() 开始绘画,您会期望在绘画完成之前已经通过 QPaintDevice 并且 QPaintEngine 处于活动状态。 您不应该在绘画处于活动状态时销毁 QPaintEngine(未调用 end())。 在示例中,QPixmap 在 QPainter 完成绘画之前被销毁。 需要在之前调用 end() 或确保像素图处于活动状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多