How do I create a default value for a QVariantMap?
-
wrote on 23 Apr 2018, 19:05 last edited by graniteDev
I have a constructor for a widget, where I would like it to take in a QVariantMap as the second argument. How do I set the default value of QVariantMap?
explicit AppConfEdit(QWidget *parent = 0, QVariantMap &AppMap = <what goes here?>);
It won't compile without a default value, but I don't know what you put for a default. Maps aren't built like strings or ints, you don't just say QMap<QString, QVariant> = {"something": 0}. I tried that, it didn't work.
-
Hi
You mean like an empty map if none is given ?
void SomeFunc( QWidget* parent, QVariantMap& Mymap = QVariantMap() ); // in.h -
wrote on 23 Apr 2018, 19:16 last edited by
That compiled, but I got 10 warnings, and we can't make pull requests on code with warnings.
C4239: nonstandard extension used : 'default argument' : conversion from 'QMap<QString,QVariant>' to 'QVariantMap &'
A non-const reference may only be bound to an lvalueC4239: nonstandard extension used : 'argument' : conversion from 'QMap<QString,QVariant>' to 'QVariantMap &'
A non-const reference may only be bound to an lvalueC4239: nonstandard extension used : 'default argument' : conversion from 'QMap<QString,QVariant>' to 'QVariantMap &'
A non-const reference may only be bound to an lvalueand another 7 more if your interested, all saying about the same thing.
-
@graniteDev said in How do I create a default value for a QVariantMap?:
QVariantMap &AppMap
It must be 'const QVariantMap &AppMap' ...
-
hmm
did you add const to it ?
i got no such warning from small test. (visual stud compiler 2015) -
hmm
did you add const to it ?
i got no such warning from small test. (visual stud compiler 2015)wrote on 23 Apr 2018, 19:24 last edited by@mrjj Ok const fixed it. Thank you!
-
@mrjj Ok const fixed it. Thank you!
@graniteDev
sorry my bad :) apparently visual stud compiled it anyway .. -
Hi,
Out of curiosity, why make it the second argument ? It's more common to have the parent as last parameter.
-
Hi,
Out of curiosity, why make it the second argument ? It's more common to have the parent as last parameter.
wrote on 23 Apr 2018, 20:10 last edited by@SGaist No reason, the parent was already there before I implemented this, so I just typed after.
-
@graniteDev
sorry my bad :) apparently visual stud compiled it anyway ..@mrjj said in How do I create a default value for a QVariantMap?:
apparently visual stud compiled it anyway ..
Yes, this is a bug in MSVC which exists for a long time. Maybe they can't fix it due to backward compatibility reasons.
-
Hi,
Out of curiosity, why make it the second argument ? It's more common to have the parent as last parameter.
wrote on 24 Apr 2018, 12:01 last edited by@SGaist This morning, turns out, a great reason arose, I don't need to overload the constructor if I put the arguments after the parent, since If I don't want to pass in a QVariantMap, I can leave it off and still provide a parent.
1/11