QSvgRenderer::load() returns a bool. In your paint event, you do not check this value. It is possible that the resource is not loading.
How have you packaged your QLed widget for use in the application? As a DLL or a static library (LIB)? If it is a static library, you may need to insert a call to initialize the resources in main(): Q_INIT_RESOURCE.
In paintEvent() this doesn't make any sense:
renderer = nullptr;
delete renderer;
renderer = nullptr;
delete renderer;
To copy to clipboard, switch view to plain text mode
If you set the pointer to null before you delete the instance, then delete does nothing and you have a memory leak. And since the renderer instance is local to the paint method, there is no need to set it to null at all. It gets re-recreated and re-initialized with every paintEvent() call. So just get rid of the "renderer = null" line completely.
I think you can simply create the QSvgRenderer instance on the stack instead of using new() to create a pointer:
{
painter.
setRenderHint(QPainter::Antialiasing,
true);
ledShapeAndColor = shapes[m_shape];
if(m_value)
ledShapeAndColor.append(colors[m_onColor]);
else
ledShapeAndColor.append(colors[m_offColor]);
bool bLoaded = renderer.load(ledShapeAndColor);
assert( bLoaded );
renderer.render(&painter);
}
void QLed::paintEvent(QPaintEvent *)
{
QSvgRenderer renderer;
QString ledShapeAndColor;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
ledShapeAndColor = shapes[m_shape];
if(m_value)
ledShapeAndColor.append(colors[m_onColor]);
else
ledShapeAndColor.append(colors[m_offColor]);
bool bLoaded = renderer.load(ledShapeAndColor);
assert( bLoaded );
renderer.render(&painter);
}
To copy to clipboard, switch view to plain text mode
One other suggestion to improve efficiency: Your two QStringList member variables should be made static const, and probably just moved to the top of the qled.cpp file. Initialize them using list initialization during compilation. In your current code, you create and initialize the lists every time a QLed is created. If your UI has a lot of LEDs, this could slow down your program's startup.
static const QStringList sShapes
{ "shape1",
"shape2",
"shape3" };
// Don't remember what your shapes and colors are :-) static const QStringList sColors
{ "color1",
"color2",
"color3" };
{
// ...
}
static const QStringList sShapes { "shape1", "shape2", "shape3" }; // Don't remember what your shapes and colors are :-)
static const QStringList sColors { "color1", "color2", "color3" };
QLed::QLed( QWidget * parent )
: QWidget( parent )
{
// ...
}
To copy to clipboard, switch view to plain text mode
Bookmarks