简体   繁体   中英

Unhandled exception while calling setFixedSize() from Qt5Widgets

I'm developing a code using Qt library in visual studio. I have a class A as Child of Qt classes.

class A::A(QWidget *parent, QGLWidget *shareWidget) : QGLWidget(parent, shareWidget)

one of the member function of this class is:

void A::setImage(Image *image)
{
    m_image = image;
    setFixedSize(image->width(), image->height());
}

(in which setFixedSize is a method of the parent class QWidget)

this method is called in the following event from another class:

bool B::event(QEvent* e)
{
    QWidget::event(e);
    ...

    A instA = new A();
    instA.setImage(*image)
    ...

}

the following exception is thrown at setFixedSize, although the passed values are really normal int like width = height = 500.

Unhandled exception at 0x54A6B056 (Qt5Widgetsd.dll):
0xC0000005: Access violation reading location 0x939394BC.

this method setImage ist called several time while runing the code, and it works just great. The problem apears only in B::event(QEvent* e).

PS: The method is not working at this point, even if I pass directly the constant values like setFixedSize(500, 500).

Looking forward to any suggestion.

A instA = new A();
instA.setImage(*image)

This looks absurd to me. Can you please give exact compiled code.

Second, while using pointers pls make sure they are not null

So your function should be like this-

void A::setImage(Image *image)
{
   if(image)  //Null check, it will enter to block only if image is not null
   {
    m_image = image;
    setFixedSize(image->width(), image->height());
   }
}

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