How to change the current time with Text
-
Hi.
This is one of the things that it's probably not completely straightforward to do in QML. Imagine that you want to display the current time, including the seconds, in QML. If you do:
@
import QtQuick 1.0Rectangle {
id: main
width: 400; height: 400Text { id: foo font.pointSize: 24 text: Date(); }
}
@Then the "text" value is not a binding, so the time is not updated each second, of course.
Any idea about how to solve it cleanly? I've tried using the Binding element, and qmlviewer doesn't throw any errors, but reading the docs doesn't seem the proper system anyway.
I thought of using a timer, but doesn't seem the proper way to do it with QML.
Any ideas?
Thank you. -
-
Yes, I suppose that there is no way to scape of the timer. A case like this can't be designed declaratively because the time is always changing, it it doesn't emit a signal all the time. :(
So, it's possible to set the timer in the QML code? Or just the C++?
-
Will this work for you?
@import QtQuick 1.0
Rectangle {
id: main
width: 400; height: 400Text { id: foo font.pointSize: 12 function set() { foo.text = Date(); } }
Timer {
id: textTimer
interval: 1000
repeat: true
running: true
triggeredOnStart: true
onTriggered: {
foo.set();
}
}
}
@ -
[quote author="disperso" date="1292182667"]But how would you set it up? I mean, in C++ I would add a line in the constructor of the widget. In QML...
Did I say that I'm quite a noob with this? :)[/quote]
QML is new so everyone is a noob. :)
-
[quote author="QtK" date="1292183966"]
[quote author="disperso" date="1292182667"]But how would you set it up? I mean, in C++ I would add a line in the constructor of the widget. In QML...Did I say that I'm quite a noob with this? :)[/quote]
QML is new so everyone is a noob. :)
[/quote]
Of course there are different levels of noob... :)
On a more serious note: it is quite amazing to me how many posts we are getting about qml... I have to say that I didn't think would be such a hit!
-
[quote author="fcrochik" date="1292185303"]I wonder if there isn't a way using an animation. I know it is overkill but it is another idea...[/quote]
Yes, I think you can animation too. I had tried it for other values but not with date. But then yes it would be a overkill.
-
Well, an animation that triggers each second and changes the second with some eye candy is quite a stress test for the app, isn't it?
I modified once "digiflip, frmo the graphics dojo":http://qt.gitorious.org/qt-labs/graphics-dojo/trees/03c1e210d0a54f7337dd953a10b66108c54ed827/digiflip to change each second, and it consumed quite a lot of CPU... and it's done in C++ (and only does that :P).
-
I assume you tested it on a PC or Laptop. On mobile device it might freeze.
-
Updating every second shouldn't stress the CPU since updates need to be every 16 ms to get smooth animations. Of course whether smooth animations are possible depends upon what is animating and the system animating it. I wouldn't expect updating text to be one of those cases.
-
Just another concern I have about QML application is the initial loading time it takes on Mobile Devices especially on higher ends like N97. Native Symbian applications load a lot more quickly. Hope this would be optimized in someway in future.
-
Updating something every second should definitely not affect CPU very much. Even on a mobile device. A small timer is very simple and not stressful. I have done much more wicked things in my QML code :P All done on CPU on a 400MHz device of course.
It takes roughly 1-2 seconds to load a QML app here. Noting that the QML app includes images that are not hinted to load asynchronously. 1-2 seconds may be slower than native Symbian apps but it's still faster than apps on most platforms including iOS, Android, etc.. so I don't think there's much problem. Just need that loading process animated.
-
For eg the flowd demo you had referred to takes more time than symbian apps to load. But once loaded it works fine.
-
[quote author="xsacha" date="1292193076"]Updating something every second should definitely not affect CPU very much. Even on a mobile device. A small timer is very simple and not stressful. I have done much more wicked things in my QML code :P All done on CPU on a 400MHz device of course.
[/quote]The digiflip demo that I mentioned runs smooth, but I have a widget that show the CPU usage, and if I made the numbers too big (or just not small), it goes up easily. As I said, it's c++, but honestly, it's code that I don't dare to understand deeply (I mean, the paint() stuff, the rest is easy of course).