Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering an SVG file with QPainter

Tags:

c++

svg

qt

I have a set of SVG files that I would like to use as icons in a QWidget. In my QWidget paintEvent function I have the following:

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:

_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?

like image 863
jasonlg3d Avatar asked Oct 19 '25 12:10

jasonlg3d


1 Answers

I found a way to do it. I created a QIcon upon construction of the object using the .svg file. I then used QPainter.drawPixmap to draw the QIcon's pixmap.

In the initializer list 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.

like image 108
jasonlg3d Avatar answered Oct 21 '25 02:10

jasonlg3d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!