2011-05-30 447 views
2

我在這裏完全喪失...的QPixmap fromImage導致分段故障

我想一個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... 

堆棧跟蹤:

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: } 
+0

QBasicAtomicInt無關緊要,因爲傳遞給它的值已經無效。這表明QImage中的QImageData,d未被初始化。我查看了QImage的所有構造函數,並且它們都初始化了d,所以它很奇怪。 – sep 2011-05-30 09:19:23

回答

1

我成功地解決了這個問題,但是我不確定潛在的問題,因爲解決方法並沒有真正地對我有很大的意義。

所以我把QImage放在/QGraphicsView上。我在場景(0,0)處有我的QImage,在位置(0,0)處有另一個QGraphicsItem。在調用QImage上的副本之前,從現場除去其他QGraphicsItem似乎解決了問題。我不知道點(0,0)有什麼特別之處。我唯一能想到的就是isNullQPoint(0,0)返回true

1

這是很難說到底是怎麼回事。然而QT文件爲QImage constructor

警告:這將創建一個QImage的 未初始化的數據。在調用填充() 以用適當的 像素值填充圖像,然後使用QPainter將其映射到 之前。

確保通過調用fill()方法正確填充您的QImage。也許這是問題的原因。