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: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

HowTo C++ Enum in QML

think I'm missing a small step

 

I'm using a enum in my App.

Defined in Header:

class MyTest: public QObject {
Q_OBJECT
Q_ENUMS(Usecase)

public:
	MyTest(bb::cascades::Application *app);
	virtual ~MyTest() {
	}

	enum Usecase {
		Test,
		Login,
		Users,
		AllFiles
	};
	// ....
};

 and I can use without problems in my Class

 

Now I added something like this

void MyTest::myMethod(Usecase usecase){
        // do some stuff
}

 and my App is set as Context

qml->setContextProperty("app", this);

 now the problem:

 

HowTo use my ENUM from QML ?

I wanto to do something like

 

    app.myMethod(Usecase.Login)

 

This doesn't work.

 

Then I tried to register the Enum similar to FilePicker does this:

// works for FilePicker
qmlRegisterUncreatableType<bb::cascades::pickers::FilePickerViewMode>(
			"bb.cascades.pickers", 1, 0, "FilePickerViewMode", "");
// this doesn't work
qmlRegisterUncreatableType<Usecase>(
			"ekkes.test.enums", 1, 0, "Usecase", "");

got a compile error

error: 'staticMetaObject' is not a member of 'MyTest::Usecase'

googled and found that this error means my Class did not inherit Q_OBJECT, but it did

 

any idea ?

-------------------------------------------------------------------------------
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,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: HowTo C++ Enum in QML

Have you checked the headers for any of the Cascades stuff that has enums? They may have an extra macro or two stuffed in somewhere on the class definition, which you'd be missing. Just an idea...

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: HowTo C++ Enum in QML

thanks for the tip - will look around if I'm finding something special

-------------------------------------------------------------------------------
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: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: HowTo C++ Enum in QML

I would put the enums in their own class with nothing else right now. Try that. Also make sure that your qml has the correct import

should have import ekkes.test.enums 1.0
Like all of my posts
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: HowTo C++ Enum in QML


kylefowler wrote:
I would put the enums in their own class with nothing else right now. Try that. Also make sure that your qml has the correct import

should have import ekkes.test.enums 1.0

QML has the correct import

 

because of Peter's hint I looked around and noticed that all Cascades Enums (per ex all FilePicker enums) are in their own Class.

 

so I'll give this a try - THX

 

-------------------------------------------------------------------------------
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: 411
Registered: ‎04-04-2012
My Device: BlackBerry Z30/Z10/Z10LE/Q10/PlayBook

Re: HowTo C++ Enum in QML

Did you ever get the enum working? Could you please tell us your resutls? Thanks

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

Re: HowTo C++ Enum in QML

Just in case somebody still needs this solved:
You can define an enum in its own header file as a QObject with Q_ENUMS, similar to Ekkes code above. You have to register it using qmlRegisterType, not uncreateable type.
----------------------------------------------------------
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: HowTo C++ Enum in QML

also learned this in the meantime ;-)

-------------------------------------------------------------------------------
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
Regular Contributor
Posts: 52
Registered: ‎02-08-2013
My Device: Z10
My Carrier: Rogers

Re: HowTo C++ Enum in QML

Not working. can't compare value in QML side:

 

console.log (ret) // print out QVariant(EnumWrapper::Enum)

if (ret == Enum.value1) // always failed.

 

any idea?

Regular Contributor
Posts: 71
Registered: ‎02-01-2013
My Device: BlackBerry Z10 Limited Edition
My Carrier: blau.de

Re: HowTo C++ Enum in QML

[ Edited ]

robinhuang wrote:

Not working. can't compare value in QML side:

 

console.log (ret) // print out QVariant(EnumWrapper::Enum)

if (ret == Enum.value1) // always failed.

 

any idea?


Try comparing to EnumWrapper.value1 instead of Enum.value1. That's what you would do in C++.

--
With best regards,
Oleksii Serdiuk