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. How do I create a default value for a QVariantMap?
Forum Updated to NodeBB v4.3 + New Features

How do I create a default value for a QVariantMap?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.5k Views 2 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.
  • G Offline
    G Offline
    graniteDev
    wrote on last edited by graniteDev
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You mean like an empty map if none is given ?
      void SomeFunc( QWidget* parent, QVariantMap& Mymap = QVariantMap() ); // in.h

      1 Reply Last reply
      0
      • G Offline
        G Offline
        graniteDev
        wrote on last edited by
        #3

        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 lvalue

        C4239: nonstandard extension used : 'argument' : conversion from 'QMap<QString,QVariant>' to 'QVariantMap &'
        A non-const reference may only be bound to an lvalue

        C4239: nonstandard extension used : 'default argument' : conversion from 'QMap<QString,QVariant>' to 'QVariantMap &'
        A non-const reference may only be bound to an lvalue

        and another 7 more if your interested, all saying about the same thing.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @graniteDev said in How do I create a default value for a QVariantMap?:

          QVariantMap &AppMap

          It must be 'const QVariantMap &AppMap' ...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            hmm
            did you add const to it ?
            i got no such warning from small test. (visual stud compiler 2015)

            G 1 Reply Last reply
            2
            • mrjjM mrjj

              hmm
              did you add const to it ?
              i got no such warning from small test. (visual stud compiler 2015)

              G Offline
              G Offline
              graniteDev
              wrote on last edited by
              #6

              @mrjj Ok const fixed it. Thank you!

              mrjjM 1 Reply Last reply
              0
              • G graniteDev

                @mrjj Ok const fixed it. Thank you!

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @graniteDev
                sorry my bad :) apparently visual stud compiled it anyway ..

                Christian EhrlicherC 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Out of curiosity, why make it the second argument ? It's more common to have the parent as last parameter.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 2 Replies Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    Out of curiosity, why make it the second argument ? It's more common to have the parent as last parameter.

                    G Offline
                    G Offline
                    graniteDev
                    wrote on last edited by
                    #9

                    @SGaist No reason, the parent was already there before I implemented this, so I just typed after.

                    1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @graniteDev
                      sorry my bad :) apparently visual stud compiled it anyway ..

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Hi,

                        Out of curiosity, why make it the second argument ? It's more common to have the parent as last parameter.

                        G Offline
                        G Offline
                        graniteDev
                        wrote on last edited by
                        #11

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