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 to use QVariantMap/QMap as property type for custom widget?
Forum Updated to NodeBB v4.3 + New Features

How to use QVariantMap/QMap as property type for custom widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 5.1k 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.
  • R Offline
    R Offline
    rickluo
    wrote on last edited by
    #1

    I'm trying to use QMap<QString, int> as custom widget property type as code snippet below, but it does not work in Qt Designer. The property will not be shown in Qt Designer Property Editor. Is it possible to make Qt Designer work well with QMap property?

    Q_PROPERTY(QMap<QString, int> map_prop_2 MEMBER map_prop_2 NOTIFY propertyChange)
    

    Thanks in advance!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by
      #2

      Hi @rickluo

      Yes ,i think that it is possible if you replace

      Q_PROPERTY(QMap<QString, int> map_prop_2 MEMBER map_prop_2 NOTIFY propertyChange)

      To

      Q_PROPERTY(QMap map_prop_2 MEMBER map_prop_2 NOTIFY propertyChange)

      You can refer to the doc from this link :

      http://doc.qt.io/qt-4.8/properties.html#requirements-for-declaring-properties

      For QMap, QList, and QValueList properties, the property value is a QVariant whose value is the entire list or map. Note that the Q_PROPERTY string cannot contain commas, because commas separate macro arguments. Therefore, you must use QMap as the property type instead of QMap<QString,QVariant>. For consistency, also use QList and QValueList instead of QList<QVariant> and QValueList<QVariant>.
      

      Hope this can help !

      R 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        The simplest way to do that is to use typedefs. Note that you also need to declare and register these types so you can use them correctly.

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

        M 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          The simplest way to do that is to use typedefs. Note that you also need to declare and register these types so you can use them correctly.

          M Offline
          M Offline
          mostefa
          wrote on last edited by
          #4

          @SGaist said in How to use QVariantMap/QMap as property type for custom widget?:

          Hi,

          The simplest way to do that is to use typedefs. Note that you also need to declare and register these types so you can use them correctly.

          I was using typedefs too , but by reading the doc this morning I thought that QMap only should be enough,

          It is not ?

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

            Take a look at the Qt 5 documentation version. It has changed since Qt 4.

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

            1 Reply Last reply
            1
            • M mostefa

              Hi @rickluo

              Yes ,i think that it is possible if you replace

              Q_PROPERTY(QMap<QString, int> map_prop_2 MEMBER map_prop_2 NOTIFY propertyChange)

              To

              Q_PROPERTY(QMap map_prop_2 MEMBER map_prop_2 NOTIFY propertyChange)

              You can refer to the doc from this link :

              http://doc.qt.io/qt-4.8/properties.html#requirements-for-declaring-properties

              For QMap, QList, and QValueList properties, the property value is a QVariant whose value is the entire list or map. Note that the Q_PROPERTY string cannot contain commas, because commas separate macro arguments. Therefore, you must use QMap as the property type instead of QMap<QString,QVariant>. For consistency, also use QList and QValueList instead of QList<QVariant> and QValueList<QVariant>.
              

              Hope this can help !

              R Offline
              R Offline
              rickluo
              wrote on last edited by
              #6

              @mostefa Sorry, I don't know how to use QMap without specifying type parameters. Do you mean replace it with QVariantMap? I tried, but still not work.
              0_1488886035145_upload-2d9c9559-90f6-484c-8427-558014e92519

              M 1 Reply Last reply
              0
              • R rickluo

                @mostefa Sorry, I don't know how to use QMap without specifying type parameters. Do you mean replace it with QVariantMap? I tried, but still not work.
                0_1488886035145_upload-2d9c9559-90f6-484c-8427-558014e92519

                M Offline
                M Offline
                mostefa
                wrote on last edited by mostefa
                #7

                @rickluo said in How to use QVariantMap/QMap as property type for custom widget?:

                @mostefa Sorry, I don't know how to use QMap without specifying type parameters. Do you mean replace it with QVariantMap? I tried, but still not work.
                0_1488886035145_upload-2d9c9559-90f6-484c-8427-558014e92519

                As suggested by @SGaist

                Forgot what i proposed,

                You can use typedef

                Your code should looks to something like this

                typedef QMap<QString, int> MapStringInt;
                Q_DECLARE_METATYPE(MapStringInt)
                
                
                Q_PROPERTY(MapStringInt name READ name WRITE setName)
                
                1 Reply Last reply
                1

                • Login

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