Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 40
Registered: ‎07-23-2012
My Device: Z10 LE, Dev Alpha C
My Carrier: AT&T

Can I use an enum as a qml property type?

Here's the scenario:  a custom component has been created in QML.  One of its properties should be constrained to specific values.  I know how to make an enum as a c++ class using Q_ENUMS that can be exposed to QML but it seems I cannot use it as a property type e.g.:

 

property MyType level: MyType.basic

 

...I get the error message:

 

    "Unable to assign int to MyType*

 

on startup even though there were no syntax errors or warning at compile time.

 

If I change it to:

 

    property int level: MyType.basic

 

it will work but then aren't I losing the type safety of using an enum?

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Can I use an enum as a qml property type?

[ Edited ]

Have you tried prefixing the type with the class name?

 

You would need to register the class as well of course.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 40
Registered: ‎07-23-2012
My Device: Z10 LE, Dev Alpha C
My Carrier: AT&T

Re: Can I use an enum as a qml property type?

[ Edited ]

Well MyType is the class name.  And the class is registered before the QMLDocument is loaded.

 

The class looks like this:

 

    #include <QtCore>

    class MyType : public QObject {
        Q_OBJECT
        Q_ENUMS(type)

    public:
        enum type {basic = 0, intermediate, advanced};
    };

 

...and it is registered like this:

 

    qmlRegisterType<MyType>("options.lib", 1, 0, "MyType");

 

...and called in the qml like this:

 

import options.lib 1.0

 

So it ought to work I think.

 

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Can I use an enum as a qml property type?

Take a look here:
http://supportforums.blackberry.com/t5/Cascades-Development/HowTo-C-Enum-in-QML/m-p/2345641#M21139
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: Can I use an enum as a qml property type?

here's my solution what I'm teaching at my Cascades Workshops

for your ENUM you only need a headerfile

 

devicemodel.hpp

#include <QtCore>

class DeviceModel: public QObject {
	Q_OBJECT
	Q_ENUMS(DeviceModelEnum)

public:
	enum DeviceModelEnum {
		Z10 = 1, Q10 = 2, Q5 = 3
	};
};

 then inside your app.cpp an include and register type

#include <devicemodel.hpp>
qmlRegisterType<DeviceModel>("ekkes_enums", 1, 0, "DeviceModel");
// before loading qml document

 and here's a working QML sample

import bb.cascades 1.0
import ekkes_enums 1.0

Page {
    Container {
        DropDown {
            Option {
                text: "Z10"
                value: DeviceModel.Z10
            }
            Option {
                text: "Q10"
                value: DeviceModel.Q10
            }
            Option {
                text: "Q5"
                value: DeviceModel.Q5
            }
            onSelectedIndexChanged: {
                deviceLabel.deviceModel = selectedValue
            }
        }
        Label {
            text: "Hello ENUM"
            textStyle.base: SystemDefaults.TextStyles.BigText
        }
        Label {
            text: "please select from DropDown"
            textStyle.base: SystemDefaults.TextStyles.SmallText
        }
        Label {
            id: deviceLabel
            property int deviceModel
            onDeviceModelChanged: {
                switch (deviceModel){
                    case DeviceModel.Z10:
                        text = "Touch Device"
                        break
                    case DeviceModel.Q10:
                    case DeviceModel.Q5:
                        text = "Keyboard Device"
                        break 
                    default: 
                    	text = "unknown"
                }
            }
        }
    }
    onCreationCompleted: {
        console.debug("Enum value "+ DeviceModel.Q10)
        deviceLabel.deviceModel = -1
    }
}

 have fun with ENUMs

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Can I use an enum as a qml property type?

@ekke,

 

The problem as I read it was trying to use the type within QML to retain type safety (whether this happens in QML when the moc file gets created I don't know), afaik the OP had no problem with using enums. Your example maps an enum to an int as well so hasn't really answered the original OP.

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Can I use an enum as a qml property type?

...and having looked around to see if this was possible (I was curious) it turns out it probably isn't.

 

https://bugreports.qt-project.org/browse/QTBUG-15483

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Can I use an enum as a qml property type?

yes, the bbry staff also replied with enums having to be mapped to int in qml in some cases.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: Can I use an enum as a qml property type?

aaah - have overread this

 

yes - there seems to be no way at the moment to use the ENUM as property type,

you can only use ENUM values

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Can I use an enum as a qml property type?

I must have skipped Simon's small posting on this next to ekkes big post. Smiley Happy

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.