【问题标题】:Unhandled exception while calling setFixedSize() from Qt5Widgets从 Qt5Widgets 调用 setFixedSize() 时出现未处理的异常
【发布时间】:2016-07-20 09:52:50
【问题描述】:

我正在使用 Visual Studio 中的 Qt 库开发代码。 我有一个 A 类作为 Qt 类的子类。

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

这个类的一个成员函数是:

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

(其中setFixedSize是父类QWidget的方法)

在以下事件中从另一个类调用此方法:

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

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

}

在 setFixedSize 处引发以下异常,尽管传递的值是真正的普通 int,例如 width = height = 500。

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

这个方法 setImage 在运行代码时被调用了好几次,而且效果很好。问题仅出现在 B::event(QEvent* e) 中。

PS:此时该方法不起作用,即使我直接传递诸如 setFixedSize(500, 500) 之类的常量值。

期待任何建议。

【问题讨论】:

  • 请改进您的问题(另见MCVE),尤其是:完成setImage 函数签名,显示B::event 的完整实现并提供完整的堆栈跟踪。

标签: c++ qt qwidget


【解决方案1】:
A instA = new A();
instA.setImage(*image)

这在我看来很荒谬。能否请给出准确的编译代码。

其次,在使用pointers时,请确保它们不是null

所以你的函数应该是这样的-

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());
   }
}

【讨论】:

  • 此时指针不为空。那一刻我可以读到。我什至确实看到了传递给 setFixedSize() 的值。但我应该承认我没有很好地总结我的代码。我应该编辑它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多