我有一组SVG文件,我想在QWidget中用作图标。在我的QWidget paintEvent函数中,我有以下内容:
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++
有人能理解这个吗?
答案 0 :(得分:1)
我找到了办法。我使用.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);
}
感谢Kuba Ober提出的建议。