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
laairoy
Posts: 24
Registered: ‎10-31-2012
My Device: Dev Alpha B

[Help] How to a Label to show the Current Time?

Hello guys,

 

I need to create an application that shows the time on the screen, I want to use a label, but do not know how to request the method of C + + QTime to show the time. Can anyone help?

 

 

Gratefull,

__
Helped you? Like me.
Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: [Help] How to a Label to show the Current Time?

a simple solution would be to create a utility method in c++ that returns a QString and mark it as Q_INVOKABLE.
The class can be propagated using qml->setContextProperty.

----------------------------------------------------------
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
Please use plain text.
Developer
laairoy
Posts: 24
Registered: ‎10-31-2012
My Device: Dev Alpha B

Re: [Help] How to a Label to show the Current Time?

I tried using "attached object", but not always get several errors, not knowing I'm creating a method and invokes it in QML.
simon_hain, Could you give an example of how to do?

gratefull,
__
Helped you? Like me.
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: [Help] How to a Label to show the Current Time?

You could just use the Date class in JavaScript, since it ought to give you the same basic features as QTime.

 


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!
Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: [Help] How to a Label to show the Current Time?

javascript date is most likely much easier, but i'll just explain how to invoke a tool method anyhow.

 

I have a class TextUtil (which is a QObject).

This propagates the class to the qml:

qml->setContextProperty("textUtil", new TextUtil());

 I use it like this:

Option {
   text: textUtil.getDefaultTimeToLiveText()
   value: "-1"
}

 To be able to call the method from QML the class has to be a QObject and you have to use Q_INVOKABLE, here is a snippet from my header:

 

class TextUtil: public QObject {
Q_OBJECT
public:
Q_INVOKABLE
QString getDefaultTimeToLiveText();

 

----------------------------------------------------------
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
Please use plain text.