Unsolved Setting and getting property on a widget...
-
I'm not sure if the way I'm doing this is correct, I try to store an ID with the widget:
QString strID(strGetAttribute(clsXMLnode::mscszAttrID)); if ( strID.isEmpty() != true ) { mvarID = strID; mpobjWidget->setProperty(clsXMLnode::mscszAttrID, mvarID); }
mvarID is a QVariant stored in the class, I'm only making it a class member as the second parameter is a reference and I'm not sure if it needs to have a life that extends the call?
clsXMLnode::mscszAttrID is a const char[] with the content "id".
When I try to retrieve the property:
QVariant varID(pobjButton->property(clsXMLnode::mscszAttrID)); if ( varID.isValid() != true ) { return; } QString strID(varID.toString()); if ( strID.isEmpty() == true ) { return; }
I can see in the debugger varID is not valid, what am I doing wrong?
-
This post is deleted! -
Hi,
There's no need for that QVariant member variable. The second parameter of the setProperty method is not just a reference, it's a const reference.
You have to check that pobjButton and mpobjWidget point to the same object.
-
@JoeCFD said in Setting and getting property on a widget...:
the properties you can define are here.
https://doc.qt.io/qt-5/qobject.html#Q_PROPERTY
if you define your own property, it may not work.
Store an ID as object name if it is not used.Please read the setProperty documentation and the part about dynamic properties.
-
@SGaist , I've modified the setProperty to:
QString strID(strGetAttribute(clsXMLnode::mscszAttrID)); if ( strID.isEmpty() != true ) { const QVariant cvarID(strID); bool blnRC(mpobjWidget->setProperty(clsXMLnode::mscszAttrID, cvarID)); qdbg() << blnRC; }
I've single stepped into setProperty and all is ok until this line is stepped over:
const int idx = d->extraData->propertyNames.indexOf(name);
On entry to the function name contains "id" and value contains "win2", however after stepping over the above, name changes to 0x0 and value changes to <null reference>.
I can see that blnRC after calling setProperty is false, so I will take a look at the documentation.
-
What do you get if you run:
QString strID(strGetAttribute(clsXMLnode::mscszAttrID)); if ( strID.isEmpty() != true ) { const QVariant cvarID(strID); mpobjWidget->setProperty(clsXMLnode::mscszAttrID, cvarID); } qDebug() << mpobjWidget->property(clsxmlnode::mscszAttrID);
-
@SGaist said in Setting and getting property on a widget...:
qDebug() << mpobjWidget->property(clsxmlnode::mscszAttrID);
I modified the code:
QString strID(strGetAttribute(clsXMLnode::mscszAttrID)); if ( strID.isEmpty() != true ) { const QVariant cvarID(strID); bool blnRC(mpobjWidget->setProperty(clsXMLnode::mscszAttrID, cvarID)); qdbg() << "HACK: " << blnRC << ", " << mpobjWidget->property(clsXMLnode::mscszAttrID); }
The output is:
S000000000003E000000000068T20:16:26.368DL00003842F../clsMainWnd.cpp[void clsXMLnode::setWidget] HACK: false, QVariant(QString, win2)
Not sure why it returns false when the value returned by property is completely correct, need to take a closer look now as why it doesn't work when I use the same code to get the property elsewhere.
-
@SPlatten If the property is defined in the class using Q_PROPERTY then true is returned on success and false otherwise. If the property is not defined using Q_PROPERTY, and therefore not listed in the meta-object, it is added as a dynamic property and false is returned.
https://doc.qt.io/qt-5/qobject.html#setProperty
your property is yours and not defined in https://doc.qt.io/qt-5/qobject.html#Q_PROPERTY.
It is ok to have false. -
@JoeCFD , thank you.
-
@SGaist I was wrong. Thanks.