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 Update on Monday, May 27th 2025

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
  • 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 23 Apr 2018, 19:05 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 23 Apr 2018, 19:09 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 23 Apr 2018, 19:16 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
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 23 Apr 2018, 19:20 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
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 23 Apr 2018, 19:20 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 23 Apr 2018, 19:24
            2
            • M mrjj
              23 Apr 2018, 19:20

              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 23 Apr 2018, 19:24 last edited by
              #6

              @mrjj Ok const fixed it. Thank you!

              M 1 Reply Last reply 23 Apr 2018, 19:24
              0
              • G graniteDev
                23 Apr 2018, 19:24

                @mrjj Ok const fixed it. Thank you!

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 23 Apr 2018, 19:24 last edited by
                #7

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

                C 1 Reply Last reply 24 Apr 2018, 04:50
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 23 Apr 2018, 19:44 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 23 Apr 2018, 20:10
                  2
                  • S SGaist
                    23 Apr 2018, 19:44

                    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 23 Apr 2018, 20:10 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
                    • M mrjj
                      23 Apr 2018, 19:24

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

                      C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 24 Apr 2018, 04:50 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
                      • S SGaist
                        23 Apr 2018, 19:44

                        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 24 Apr 2018, 12:01 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

                        1/11

                        23 Apr 2018, 19:05

                        • Login

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