Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I want to passing a variable/function-return as a template argument
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Prmego
    wrote on last edited by Prmego
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • Vinod KuntojiV Offline
      Vinod KuntojiV Offline
      Vinod Kuntoji
      wrote on last edited by Vinod Kuntoji
      #2

      @Prmego ,

      Try this, it will work,

      Q_FOREACH(const QVariant &option, options){
      setting.setValue(options.key(option),options.value(option.typeName()));
      }

      C++, Qt, Qt Quick Developer,
      PthinkS, Bangalore

      P 1 Reply Last reply
      0
      • P Prmego

        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?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        0
        • T Offline
          T Offline
          tomma
          wrote on last edited by
          #4
          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()

          P 1 Reply Last reply
          3
          • Vinod KuntojiV Vinod Kuntoji

            @Prmego ,

            Try this, it will work,

            Q_FOREACH(const QVariant &option, options){
            setting.setValue(options.key(option),options.value(option.typeName()));
            }

            P Offline
            P Offline
            Prmego
            wrote on last edited by Prmego
            #5

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

            1 Reply Last reply
            0
            • jsulmJ jsulm

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

              P Offline
              P Offline
              Prmego
              wrote on last edited by
              #6

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

              E 1 Reply Last reply
              0
              • P Prmego

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

                E Offline
                E Offline
                Eligijus
                wrote on last edited by
                #7

                @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>());
                }
                
                P 1 Reply Last reply
                0
                • T tomma
                  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()

                  P Offline
                  P Offline
                  Prmego
                  wrote on last edited by Prmego
                  #8

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

                  1 Reply Last reply
                  1
                  • E Eligijus

                    @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>());
                    }
                    
                    P Offline
                    P Offline
                    Prmego
                    wrote on last edited by Prmego
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved