I want to passing a variable/function-return as a template argument



  • I have a QVariantHash container which stores some objects like QColor, 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 a setting.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 method const 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()));
    }


  • Moderators

    @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()



  • @Vinod-Kuntoji : Thanks, but unfortunately, it returns QVariant(Invalid) for each object.



  • @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.



  • @Prmego

    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>());
    }
    


  • @tomma : I think your answer is the most appropriate solution.
    I thank you for your response.



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.