2014-04-03 79 views
0

我有一組SVG文件,我想在QWidget中用作圖標。在我的QWidget的paintEvent函數我有以下幾點:用QPainter呈現SVG文件

void Tool::paintEvent(QPaintEvent *e) { 
    QImage icon = QImage(32, 32, QImage::Format_ARGB32); 
    QSvgRenderer *ren = new QSvgRenderer(QString(m_part_dir.c_str()) + "\\drawing.svg"); 
    QPainter painter(&icon); 
    ren->render(&painter); 
} 

我已經看過關於如何做到這一點有多條建議,這方法保持上來。但是,當我運行這段代碼時,它會引發異常。上線:

ren->render(&painter); 

斷裂停止在一個名爲dbgrptt.c文件:

_CRTIMP void __cdecl _CrtDbgBreak(
    void 
    ) 
{ 
    __debugbreak(); 
} 

調用堆棧看起來是這樣的:

msvcr110d.dll!_CrtDbgBreak() Line 87 C 
msvcr110d.dll!_VCrtDbgReportW(int nRptType=2, void * returnAddress=0x0f8a7553, const wchar_t * szFile=0x0f793148, int nLine=52, const wchar_t * szModule=0x00000000, const wchar_t * szFormat=0x0f77127c, char * arglist=0x0029e954) Line 506 C 
msvcr110d.dll!_CrtDbgReportWV(int nRptType=2, void * returnAddress=0x0f8a7553, const wchar_t * szFile=0x0f793148, int nLine=52, const wchar_t * szModule=0x00000000, const wchar_t * szFormat=0x0f77127c, char * arglist=0x0029e954) Line 262 C++ 
msvcr110d.dll!_CrtDbgReportW(int nRptType=2, const wchar_t * szFile=0x0f793148, int nLine=52, const wchar_t * szModule=0x00000000, const wchar_t * szFormat=0x0f77127c, ...) Line 279 C++ 
msvcr110d.dll!operator delete(void * pUserData=0x031392e0) Line 52 C++ 
QtGuid4.dll!QPenPrivate::`scalar deleting destructor'(unsigned int) C++ 
QtGuid4.dll!QPen::~QPen() Line 346 C++ 
QtGuid4.dll!QPaintEngineExPrivate::~QPaintEngineExPrivate() Line 172 C++ 
QtGuid4.dll!QRasterPaintEnginePrivate::~QRasterPaintEnginePrivate() C++ 
QtGuid4.dll!QRasterPaintEnginePrivate::`scalar deleting destructor'(unsigned int) C++ 
QtGuid4.dll!QScopedPointerDeleter<QPaintEnginePrivate>::cleanup(QPaintEnginePrivate * pointer=0x03135d80) Line 62 C++ 
QtGuid4.dll!QScopedPointer<QPaintEnginePrivate,QScopedPointerDeleter<QPaintEnginePrivate> >::~QScopedPointer<QPaintEnginePrivate,QScopedPointerDeleter<QPaintEnginePrivate> >() Line 100 C++ 
QtGuid4.dll!QPaintEngine::~QPaintEngine() Line 723 C++ 
QtGuid4.dll!QPaintEngineEx::~QPaintEngineEx() C++ 
QtGuid4.dll!QRasterPaintEngine::~QRasterPaintEngine() Line 428 C++ 
QtGuid4.dll!QRasterPaintEngine::`scalar deleting destructor'(unsigned int) C++ 
QtGuid4.dll!QImageData::~QImageData() Line 224 C++ 
QtGuid4.dll!QImageData::`scalar deleting destructor'(unsigned int) C++ 
QtGuid4.dll!QImage::~QImage() Line 1283 C++ 
SystemDesigner_d.exe!Parts::Tool::paintEvent(QPaintEvent * e=0x0029f238) Line 39 C++ 

任何人都可以理解這一點?

+1

嘗試在繪畫事件之外渲染圖像,並且僅在繪製事件期間繪製預渲染的圖像。即使這不是問題,在繪畫事件中打開文件是一個禁忌。它應該儘快完成。 –

回答

0

我找到了一個辦法。我使用.svg文件構建對象時創建了一個QIcon。然後我使用QPainter.drawPixmap來繪製QIcon的像素圖。

在初始化列表m_icon(QString(m_part_dir.c_str()) + "\\icon.svg")

Paint事件:

void Tool::paintEvent(QPaintEvent *e) { 
    QPainter painter(this); 
    painter.setPen(QPen(Qt::gray, 1)); 
    painter.drawRect(0, 0, TOOL_WIDTH-1, TOOL_HEIGHT); 

    //QIcon icon(QString(m_part_dir.c_str()) + "\\icon.svg"); 
    painter.drawPixmap(4, 4, 32, 32, m_icon.pixmap(QSize(32,32))); 

    int text_y = TOOL_HEIGHT/2 + 3; 
    painter.setPen(QPen(Qt::black, 1)); 
    QString name(m_data["name"].c_str()); 
    painter.drawText(40, text_y, name); 
} 

由於庫巴奧伯來回的建議。