Animating SVG widget
-
I have instances of QSvgWidget which create from a resource in my application:
psvgRxIcon = new QSvgWidget(":/SVG_LED"); psvgRxIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); psvgRxIcon->setFixedSize(24, 24);
I'm investigating the best way to animate and thanks to suggestions from @mrjj, I've added:
pdocRxIcon = new QDomDocument(); pfileRxIcon = new QFile(":/SVG_LED"); pfileRxIcon->open(QIODevice::ReadOnly | QIODevice::Text); pdocRxIcon->setContent(pfileRxIcon);
I've found the element I want to change, changed it and put it back in the attribute, the question is do I then have to re-create the widget every-time I change the document? Is there another way?
There are no methods that return pointers or references in the document so its a bit of a pain to modify and put back. I have use setAttribute to put back the modified attribute back into the element.
-
Done it, and it works, in the SVG resource I set the opacity to 0, then in this modification I change the opacity to 1:
double dblOpacity = 1.0; QDomElement elRoot(mpdocRxIcon->firstChildElement()); QDomNodeList lstNodes(elRoot.elementsByTagName(TraineeMonitor::mscszTagStop)); bool blnAssign(false); for( int i=0; i<lstNodes.size(); i++ ) { QDomNode ndStop(lstNodes.at(i)); if ( !ndStop.isElement() ) { continue; } QDomElement elm(ndStop.toElement()); QString strID(elm.attribute(TraineeMonitor::mscszAttrID)); if ( strID.compare(TraineeMonitor::mscszLEDcolor) == 0 ) { QString strStyle(elm.attribute(TraineeMonitor::mscszAttrStyle)); QStringList slstStyles(strStyle.split(TraineeMonitor::mscszStyleDelimiter)); uint uintIdx = 0; foreach( QString strStyle, slstStyles ) { QStringList slstStyle(strStyle.split(TraineeMonitor::mscszAttrDelimiter)); strStyle = slstStyle[0]; if ( strStyle.compare(TraineeMonitor::mscszStyleStopOpacity) == 0 ) { slstStyles[uintIdx] = strStyle + TraineeMonitor::mscszAttrDelimiter + QString::number(dblOpacity); blnAssign = true; break; } uintIdx++; } if ( blnAssign == true ) { QString strNewStyle; foreach( QString strStyle, slstStyles ) { if ( strNewStyle.isEmpty() != true ) { strNewStyle += TraineeMonitor::mscszStyleDelimiter; } strNewStyle += strStyle; } elm.setAttribute(TraineeMonitor::mscszAttrStyle, strNewStyle); break; } } } if ( blnAssign == true ) { mpsvgRxIcon->load(mpdocRxIcon->toByteArray()); }
The final version will have a QTimer that increments the sets the opacity to 1 then decrements to 0 to fade out.