gpt4 book ai didi

c++ - QImage::load 导致应用程序崩溃

转载 作者:太空狗 更新时间:2023-10-29 23:08:59 26 4
gpt4 key购买 nike

我不知道为什么,但这段代码导致我的应用程序崩溃。当我在 setPicture 方法中使用一个全新的指向 QImage 的指针时,它没有!什么会导致这种行为?

Canvas 类:

#include <QtGui>

class Canvas : public QWidget
{
Q_OBJECT

public:
QImage *p;

Canvas();
void setPicture(QString);
};

Canvas::Canvas()
{

}

void Canvas::setPicture(QString filename)
{
// This causes crash.
this->p = new QImage;
this->p->load(filename);

// This does not cause crash.Why?
//QImage *z = new QImage;
//z->load(filename);
}

这是窗口类:

#include <QtGui>

#include "Canvas.h"

class Window : public QWidget
{
Q_OBJECT

private:
Canvas *preview;

public:
Window();

public slots:
void browseFile();
};

Window::Window()
{
QGridLayout *layout = new QGridLayout;
Canvas *preview = new Canvas;
preview->setMinimumSize(400,200);
QSlider *slider = new QSlider;
slider->setOrientation(Qt::Horizontal);
QPushButton *browse = new QPushButton("Browse...");

layout->addWidget(preview, 1, 1);
//layout->addWidget(slider, 1, 2);
layout->addWidget(browse, 2, 2);

this->setLayout(layout);

this->resize(600,300);

QObject::connect(browse, SIGNAL(clicked()), SLOT(browseFile()));

}

void Window::browseFile()
{
QString filename;

filename = QFileDialog::getOpenFileName(this, "Open Picture", "", "Image Files (*.png *.jpg *.bmp)");

if(!filename.isEmpty())
{
qDebug() << "filename: "+filename;
preview->setPicture(filename);
//preview->repaint();

}
}

调用堆栈跟踪...

0 Canvas::setPicture   Canvas.h   25   0x100003410 
1 Window::browseFile Window.h 52 0x1000038c1
2 Window::qt_metacall moc_Window.cpp 72 0x1000025c8
3 QMetaObject::activate 0 0x100c93ac2
4 QAbstractButton::clicked 0 0x10063f2ed
5 QAbstractButtonPrivate::emitClicked 0 0x1003bc61e
6 QAbstractButtonPrivate::click 0 0x1003bd394
7 QAbstractButton::mouseReleaseEvent 0 0x1003bd556
8 QWidget::event 0 0x1000d2a52
9 QAbstractButton::event 0 0x1003bc5e6
10 QPushButton::event 0 0x100448ad2
11 QApplicationPrivate::notify_helper 0 0x100086e48
12 QApplication::notify 0 0x1000877a8
13 QCoreApplication::notifyInternal 0 0x100c805c6
14 qt_sendSpontaneousEvent 0 0x1000865da
15 qt_mac_handleMouseEvent 0 0x10004130a
16 -[QCocoaView mouseUp:] 0 0x100034be6
17 -[NSWindow sendEvent:] 0 0x7fff8ca74568
18 -[QCocoaWindow sendEvent:] 0 0x100039795
19 -[NSApplication sendEvent:] 0 0x7fff8ca0cd4d
20 -[QNSApplication sendEvent:] 0 0x10003cc1b
21 -[NSApplication run] 0 0x7fff8c9a325f
22 QEventDispatcherMac::processEvents 0 0x100044e7b
23 QEventLoop::exec 0 0x100c7dc55
24 QCoreApplication::exec 0 0x100c80bff
25 main main.cpp 12 0x100002cc0

最佳答案

指向 Canvas 类的指针是在 Window 类的构造函数中声明和实例化的,而不是使用私有(private)声明的指针。

如果我没记错的话,在调用 browseFile 方法时,窗口构造函数中指向 Canvas 的指针超出范围。

应该是:

class Window : public QWidget
{
Q_OBJECT

private:
Canvas *preview;

public:
Window();

public slots:
void browseFile();
};

Window::Window()
{
QGridLayout *layout = new QGridLayout;
preview = new Canvas;
....

虽然 setPicture 是如何被调用的对我来说是个谜!

经验教训:不要在您计划稍后使用的构造函数中声明任何内容。

关于c++ - QImage::load 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7185159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com