简体   繁体   English

使用QPainter渲染SVG文件

[英]Rendering an SVG file with QPainter

I have a set of SVG files that I would like to use as icons in a QWidget. 我有一组要用作QWidget中图标的SVG文件。 In my QWidget paintEvent function I have the following: 在我的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);
}

I have looked at multiple suggestions on how to do this and this method keeps coming up. 我已经查看了有关如何执行此操作的多个建议,并且这种方法不断出现。 However, when i run this code it throws an exception. 但是,当我运行此代码时,它将引发异常。 on the line: 在线上:

ren->render(&painter);

The break stops in a file called dbgrptt.c: 中断在名为dbgrptt.c的文件中停止:

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

The call stack looks like this: 调用堆栈如下所示:

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++

Can anyone make sense of this? 任何人都可以理解吗?

I found a way to do it. 我找到了一种方法。 I created a QIcon upon construction of the object using the .svg file. 我使用.svg文件在构造对象时创建了一个QIcon。 I then used QPainter.drawPixmap to draw the QIcon's pixmap. 然后,我使用QPainter.drawPixmap绘制QIcon的像素图。

In the initializer list m_icon(QString(m_part_dir.c_str()) + "\\\\icon.svg") 在初始化程序列表中, m_icon(QString(m_part_dir.c_str()) + "\\\\icon.svg")

the Paint event: 绘画事件:

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

Thanks Kuba Ober fro the suggestion. 感谢库巴·奥伯的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM