QPixmap fromImage 导致分割错误

发布于 2024-11-10 10:12:47 字数 4139 浏览 7 评论 0原文

我在这里完全不知所措...

我正在尝试将 QImage 转换为 QPixmap 但我的程序不断崩溃并出现以下堆栈跟踪。奇怪的是,如果我指定的 QRectF 没有左上角点 (0, 0),它就可以正常工作。我知道 QImage 也有一个复制方法,但这也失败了(没有有用的堆栈跟踪),所以我想我应该尝试使用 Pixmaps 来代替... Line 的 4/5 堆栈跟踪是我的,但我'我基本上执行以下

QImage _drawing = QImage(1024, 1024);
// fill _drawing with some data

QRect i_work = QRect(1, 1, 10, 10);
QRect i_break = QRect(0, 0, 10, 10);

QPixmap foo = QPixmap::fromImage(_drawing);
// QPixmap good = foo.copy(i_work); // I work
QPixmap broken = foo.copy(i_break); // Segmentation fault...

StackTrace:

Program received signal SIGSEGV, Segmentation fault.
0x01f44024 in QBasicAtomicInt::ref (this=0xabababab)
    at ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h:120
120     ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h: No such file
 or directory.
        in ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h
(gdb) bt
#0  0x01f44024 in QBasicAtomicInt::ref (this=0xabababab)
    at ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h:120
#1  0x01964d3e in QImage::QImage (this=0x28cc70, image=...)
    at image\qimage.cpp:1125
#2  0x01990682 in QRasterPixmapData::fromImage (this=0xc391a00,
    sourceImage=..., flags=...) at image\qpixmap_raster.cpp:151
#3  0x019862ab in QPixmap::fromImage (image=..., flags=...)
    at image\qpixmap.cpp:2031
#4  0x00423fdc in TxImage::getSelection (this=0xc3b6cb0, selection_area=...)
    at TxImage.cpp:207
#5  0x004421e2 in RomView::mouseReleaseEvent (this=0xc397978, event=0x28d5fc)
    at RomView.cpp:261
#6  0x019241a0 in QWidget::event (this=0xc397978, event=0x28d5fc)
    at kernel\qwidget.cpp:8187
#7  0x01c9e6bc in QFrame::event (this=0xc397978, e=0x28d5fc)
    at widgets\qframe.cpp:557
#8  0x01d2bf0f in QAbstractScrollArea::viewportEvent (this=0xc397978,
    e=0x28d5fc) at widgets\qabstractscrollarea.cpp:1043
#9  0x01e9aae6 in QGraphicsView::viewportEvent (this=0xc397978,
    event=0x28d5fc) at graphicsview\qgraphicsview.cpp:2862
#10 0x01f86a13 in QAbstractScrollAreaPrivate::viewportEvent (this=0xc3969f8,
    event=0x28d5fc) at widgets//qabstractscrollarea_p.h:100
#11 0x01f8506c in QAbstractScrollAreaFilter::eventFilter (this=0xbfa7388, o=
    0xbf43978, e=0x28d5fc) at widgets//qabstractscrollarea_p.h:116
#12 0x6a1ffc73 in QCoreApplicationPrivate::sendThroughObjectEventFilters (
    this=0xa7a46d8, receiver=0xbf43978, event=0x28d5fc)
    at kernel\qcoreapplication.cpp:847
#13 0x018d96e5 in QApplicationPrivate::notify_helper (this=0xa7a46d8,
    receiver=0xbf43978, e=0x28d5fc) at kernel\qapplication.cpp:4392
#14 0x018d7909 in QApplication::notify (this=0x28fe34, receiver=0xbf43978,
    e=0x28d5fc) at kernel\qapplication.cpp:3959
#15 0x6a1ff9dc in QCoreApplication::notifyInternal (this=0x28fe34,
    receiver=0xbf43978, event=0x28d5fc) at kernel\qcoreapplication.cpp:732
#16 0x01f4d53e in QCoreApplication::sendSpontaneousEvent (receiver=0xbf43978,
    event=0x28d5fc)
    at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:218
#17 0x018d6118 in QApplicationPrivate::sendMouseEvent (receiver=0xbf43978,
    event=0x28d5fc, alienWidget=0xc3adb70, nativeWidget=0x28fd60,
    buttonDown=0x237941c, lastMouseReceiver=..., spontaneous=true)
    at kernel\qapplication.cpp:3056
#18 0x0193fc63 in QETWidget::translateMouseEvent (this=0x28fd60, msg=...)
    at kernel\qapplication_win.cpp:3317
#19 0x0193aaa6 in QtWndProc (hwnd=0x130fb8, message=514, wParam=0,
    lParam=5373956) at kernel\qapplication_win.cpp:1657
#20 0x762c62fa in USER32!OffsetRect () from C:\Windows\syswow64\user32.dll
#21 0x900010c2 in ?? ()
#22 0x90909090 in ?? ()
#23 0x00df7d80 in operator+ ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb)

我向您保证 qatomic_i386.h 存在并查看第 120 行...

112: inline bool QBasicAtomicInt::ref()
113: {
114:     unsigned char ret;
115:    asm volatile("lock\n"
116:                 "incl %0\n"
117:                 "setne %1"
118:                 : "=m" (_q_value), "=qm" (ret)
119:                 : "m" (_q_value)
120:                 : "memory");
121:    return ret != 0;
122: }

I'm at a complete loss here...

I'm trying to convert a QImage to a QPixmap but my program keeps crashing with the following stack trace. What's weird is it works fine if the QRectF that I specify doesn't have an upper left point of (0, 0). I know QImage has a copy method as well, but that was also failing (with no useful stacktrace), so I figured I'd try to work with Pixmaps instead... Line's 4/5 of the stacktrace are mine, but I'm basically doing the following

QImage _drawing = QImage(1024, 1024);
// fill _drawing with some data

QRect i_work = QRect(1, 1, 10, 10);
QRect i_break = QRect(0, 0, 10, 10);

QPixmap foo = QPixmap::fromImage(_drawing);
// QPixmap good = foo.copy(i_work); // I work
QPixmap broken = foo.copy(i_break); // Segmentation fault...

StackTrace:

Program received signal SIGSEGV, Segmentation fault.
0x01f44024 in QBasicAtomicInt::ref (this=0xabababab)
    at ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h:120
120     ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h: No such file
 or directory.
        in ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h
(gdb) bt
#0  0x01f44024 in QBasicAtomicInt::ref (this=0xabababab)
    at ../../include/QtCore/../../src/corelib/arch/qatomic_i386.h:120
#1  0x01964d3e in QImage::QImage (this=0x28cc70, image=...)
    at image\qimage.cpp:1125
#2  0x01990682 in QRasterPixmapData::fromImage (this=0xc391a00,
    sourceImage=..., flags=...) at image\qpixmap_raster.cpp:151
#3  0x019862ab in QPixmap::fromImage (image=..., flags=...)
    at image\qpixmap.cpp:2031
#4  0x00423fdc in TxImage::getSelection (this=0xc3b6cb0, selection_area=...)
    at TxImage.cpp:207
#5  0x004421e2 in RomView::mouseReleaseEvent (this=0xc397978, event=0x28d5fc)
    at RomView.cpp:261
#6  0x019241a0 in QWidget::event (this=0xc397978, event=0x28d5fc)
    at kernel\qwidget.cpp:8187
#7  0x01c9e6bc in QFrame::event (this=0xc397978, e=0x28d5fc)
    at widgets\qframe.cpp:557
#8  0x01d2bf0f in QAbstractScrollArea::viewportEvent (this=0xc397978,
    e=0x28d5fc) at widgets\qabstractscrollarea.cpp:1043
#9  0x01e9aae6 in QGraphicsView::viewportEvent (this=0xc397978,
    event=0x28d5fc) at graphicsview\qgraphicsview.cpp:2862
#10 0x01f86a13 in QAbstractScrollAreaPrivate::viewportEvent (this=0xc3969f8,
    event=0x28d5fc) at widgets//qabstractscrollarea_p.h:100
#11 0x01f8506c in QAbstractScrollAreaFilter::eventFilter (this=0xbfa7388, o=
    0xbf43978, e=0x28d5fc) at widgets//qabstractscrollarea_p.h:116
#12 0x6a1ffc73 in QCoreApplicationPrivate::sendThroughObjectEventFilters (
    this=0xa7a46d8, receiver=0xbf43978, event=0x28d5fc)
    at kernel\qcoreapplication.cpp:847
#13 0x018d96e5 in QApplicationPrivate::notify_helper (this=0xa7a46d8,
    receiver=0xbf43978, e=0x28d5fc) at kernel\qapplication.cpp:4392
#14 0x018d7909 in QApplication::notify (this=0x28fe34, receiver=0xbf43978,
    e=0x28d5fc) at kernel\qapplication.cpp:3959
#15 0x6a1ff9dc in QCoreApplication::notifyInternal (this=0x28fe34,
    receiver=0xbf43978, event=0x28d5fc) at kernel\qcoreapplication.cpp:732
#16 0x01f4d53e in QCoreApplication::sendSpontaneousEvent (receiver=0xbf43978,
    event=0x28d5fc)
    at ../../include/QtCore/../../src/corelib/kernel/qcoreapplication.h:218
#17 0x018d6118 in QApplicationPrivate::sendMouseEvent (receiver=0xbf43978,
    event=0x28d5fc, alienWidget=0xc3adb70, nativeWidget=0x28fd60,
    buttonDown=0x237941c, lastMouseReceiver=..., spontaneous=true)
    at kernel\qapplication.cpp:3056
#18 0x0193fc63 in QETWidget::translateMouseEvent (this=0x28fd60, msg=...)
    at kernel\qapplication_win.cpp:3317
#19 0x0193aaa6 in QtWndProc (hwnd=0x130fb8, message=514, wParam=0,
    lParam=5373956) at kernel\qapplication_win.cpp:1657
#20 0x762c62fa in USER32!OffsetRect () from C:\Windows\syswow64\user32.dll
#21 0x900010c2 in ?? ()
#22 0x90909090 in ?? ()
#23 0x00df7d80 in operator+ ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb)

I assure you that qatomic_i386.h exists and looking at line 120...

112: inline bool QBasicAtomicInt::ref()
113: {
114:     unsigned char ret;
115:    asm volatile("lock\n"
116:                 "incl %0\n"
117:                 "setne %1"
118:                 : "=m" (_q_value), "=qm" (ret)
119:                 : "m" (_q_value)
120:                 : "memory");
121:    return ret != 0;
122: }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2024-11-17 10:12:47

很难判断到底发生了什么。然而QT Doc for QImage构造函数

警告:这将创建一个 QImage
带有未初始化的数据。调用 fill()
用适当的填充图像
绘制之前的像素值
使用 QPainter。

通过调用 fill() 方法确保您的 QImage 被正确填充。也许这就是问题的原因。

It is very difficult to tell what is going on. However QT Doc for QImage constructor says that

Warning: This will create a QImage
with uninitialized data. Call fill()
to fill the image with an appropriate
pixel value before drawing onto it
with QPainter.

Ensure that your QImage is filled properly by calling fill () method. Maybe this is the cause of the problem.

四叶草在未来唯美盛开 2024-11-17 10:12:47

我成功地解决了这个问题,但我不确定根本的问题,因为解决方法对我来说并没有多大意义。

所以我在QGraphicsScene/QGraphicsView上渲染QImage。我的 QImage 位于场景中的位置 (0,0),另一个 QGraphicsItem 也位于位置 (0,0)。在对 QImage 调用复制之前从场景中删除另一个 QGraphicsItem 似乎可以解决该问题。我不知道 (0,0) 点有什么特别之处。我唯一能想到的是 QPoint(0,0) 上的 isNull 返回 true

I was successfully able to solve the problem, but I'm not sure of the underlying problem, as the workaround doesn't really make a whole lot of sense to me.

So I was rendering the QImage on a QGraphicsScene/QGraphicsView. I had my QImage at location (0,0) on the scene and another QGraphicsItem also at location (0,0). Removing the other QGraphicsItem from the scene before calling copy on the QImage seemed to fix the problem. I've no idea what's so special about the point (0,0). The only thing I can think of is that isNull on a QPoint(0,0) returns true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文