I want to passing a variable/function-return as a template argument
-
I have a
QVariantHash
container which stores some objects likeQColor
,QFont
, etc.
but note that I don't know what the container contains I just told you an example of the objects types that the container can contain.
Those objects I'm trying to store them in asetting.ini
file.QSettings setting("setting.ini", QSettings::IniFormat); QVariantHash options; options.insert("fontStyle", fontStyleObject); // QFont fontStyleObject; options.insert("fontColor", fontColorObject); // QColor fontColorObject; Q_FOREACH(const QVariant &option, options){ setting->setValue(options.key(option), option.value<option.typeName()>()); }
The problem lies in this part
option.value<option.typeName()>()
.
I want to retrieve each object as before by using the methodconst char *QVariant::typeName()
which returns the type of each object.Unfortunately, there is an error message
error: C2974: 'QVariant::value': invalid template argument for 'T', type expected
, because of<option.typeName()>
doen't work as a template argument.Is there any solution to continue and achieve this idea?
-
@Prmego ,
Try this, it will work,
Q_FOREACH(const QVariant &option, options){
setting.setValue(options.key(option),options.value(option.typeName()));
} -
I have a
QVariantHash
container which stores some objects likeQColor
,QFont
, etc.
but note that I don't know what the container contains I just told you an example of the objects types that the container can contain.
Those objects I'm trying to store them in asetting.ini
file.QSettings setting("setting.ini", QSettings::IniFormat); QVariantHash options; options.insert("fontStyle", fontStyleObject); // QFont fontStyleObject; options.insert("fontColor", fontColorObject); // QColor fontColorObject; Q_FOREACH(const QVariant &option, options){ setting->setValue(options.key(option), option.value<option.typeName()>()); }
The problem lies in this part
option.value<option.typeName()>()
.
I want to retrieve each object as before by using the methodconst char *QVariant::typeName()
which returns the type of each object.Unfortunately, there is an error message
error: C2974: 'QVariant::value': invalid template argument for 'T', type expected
, because of<option.typeName()>
doen't work as a template argument.Is there any solution to continue and achieve this idea?
@Prmego It cannot work like you want - templates are instantiated at compile time. That means the type T must be known at compile time. If you do
option.value<option.typeName()>();
then the type for T is not known at compile time, so the compiler cannot instantiate the template for unknown type.
-
Q_FOREACH(const QString &option, options.keys()){ setting->setValue(option, options.value(option)); }
QSettings takes and returns QVariant, there is no need to overcomplicate things.
QSettings::setValue(const QString &key, const QVariant &value)If you need to know type of QVariant then you can check QVariant::type()
-
@Prmego ,
Try this, it will work,
Q_FOREACH(const QVariant &option, options){
setting.setValue(options.key(option),options.value(option.typeName()));
}@Vinod-Kuntoji : Thanks, but unfortunately, it returns
QVariant(Invalid)
for each object. -
@Prmego It cannot work like you want - templates are instantiated at compile time. That means the type T must be known at compile time. If you do
option.value<option.typeName()>();
then the type for T is not known at compile time, so the compiler cannot instantiate the template for unknown type.
-
@jsulm : Thank you, I have known that recently (Templates are instantiated at compile time).
But I have asked my question here to find a solution to that problem. -
Q_FOREACH(const QString &option, options.keys()){ setting->setValue(option, options.value(option)); }
QSettings takes and returns QVariant, there is no need to overcomplicate things.
QSettings::setValue(const QString &key, const QVariant &value)If you need to know type of QVariant then you can check QVariant::type()
-
Q_FOREACH(const QVariant &option, options){ if(option.typeName() == "QFont") setting->setValue(options.key(option), option.value<QFont>()); if(option.typeName() == "QColor") setting->setValue(options.key(option), option.value<QColor>()); }
@Eligijus : Thank you, but I think you forget something I wrote in my question, look at the following quote:
Note that I don't know what the container contains I just told you an example of the objects types that the container can contain.
Anyway, I thank you.