Unsolved How to use QVariantMap/QMap as property type for custom widget?
-
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!
-
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 !
-
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.
-
@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 ?
-
Take a look at the Qt 5 documentation version. It has changed since Qt 4.
-
@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.
-
@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.
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)