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

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

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 2.1k Views
  • 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 7 Jul 2017, 02:06 last edited by Prmego 7 Jul 2017, 02:09
    #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?

    J 1 Reply Last reply 7 Jul 2017, 04:21
    0
    • V Offline
      V Offline
      Vinod Kuntoji
      wrote on 7 Jul 2017, 03:22 last edited by Vinod Kuntoji 7 Jul 2017, 03:24
      #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 7 Jul 2017, 12:47
      0
      • P Prmego
        7 Jul 2017, 02:06

        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?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 7 Jul 2017, 04:21 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 7 Jul 2017, 12:55
        0
        • T Offline
          T Offline
          tomma
          wrote on 7 Jul 2017, 06:27 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 7 Jul 2017, 13:23
          3
          • V Vinod Kuntoji
            7 Jul 2017, 03:22

            @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 7 Jul 2017, 12:47 last edited by Prmego 7 Jul 2017, 12:48
            #5

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

            1 Reply Last reply
            0
            • J jsulm
              7 Jul 2017, 04:21

              @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 7 Jul 2017, 12:55 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 7 Jul 2017, 13:06
              0
              • P Prmego
                7 Jul 2017, 12:55

                @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 7 Jul 2017, 13:06 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 7 Jul 2017, 13:31
                0
                • T tomma
                  7 Jul 2017, 06:27
                  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 7 Jul 2017, 13:23 last edited by Prmego 7 Jul 2017, 13:24
                  #8

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

                  1 Reply Last reply
                  1
                  • E Eligijus
                    7 Jul 2017, 13:06

                    @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 7 Jul 2017, 13:31 last edited by Prmego 7 Jul 2017, 13:34
                    #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

                    8/9

                    7 Jul 2017, 13:23

                    • Login

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