[Qt-qml] Timer without animation ?

Gregory Schlomoff gregory.schlomoff at gmail.com
Mon Jan 10 12:54:32 CET 2011


Strange...

I'm reposting it here, then...
Sorry for the inconvenience, and hope that this helps

==== Original post ====

Hello,

The answer is yes.

One thing that QML developpers tend to forget easily is that nesting
elements inside others is not a "native" feature of the language.
Instead, it works through the default property mechanism.

Let's consider an example. When you write:

Rectangle {
  Timer {...}
}
The QML engine actually interprets that as:
Rectangle {
  data: [
   Timer {...}
  ]
}

But because "data" has been declared as the default property, you
don't need to specify it. You just write your objects one inside the
other.

"data" is a property of Item [1], and since all visual items inherit
from Item, now you know why you can put a Timer inside any visual
item. QtObject, on the other hand, has only one property: QString
objectName. This is why you cannot put the timer inside it.

Now, if you want to put a Timer inside a QtObject, you at least two options :

=== Option 1 : inside a dedicated property ===
QtObject {
  property Timer myTimer: Timer { ... }
}

=== Option 2 : inside a generic property ===
QtObject {
  property list<QtObject> data
  data: [
   Timer { id: myTimer; ... }
 ]
}

Etc...

The best solution would be to use the second case and to declare
"data" as the default property, so that you actually get the same
syntaxt as with visual items, but for some reason it's not working. I
guess if you create your own QObject wrapper in C++ and declare the
default property there, it will be ok.

Cheers,

Greg

[1] see: http://doc.qt.nokia.com/main-snapshot/qml-item.html


On Mon, Jan 10, 2011 at 6:50 PM,  <petrus.lundqvist at nokia.com> wrote:
> I have not seen your post. Maybe it was lost or delayed somehow? Note the full quote chain in my email. I only saw Alan's post, which is what I was answering to. The confusion stems from the fact that my reply seems to have accidentally been targeted at your post rather than Alan's. Either way, I can't see your post with the in-depth explanation anywhere.
>
> Peppe
>
> ________________________________________
> From: ext Gregory Schlomoff [gregory.schlomoff at gmail.com]
> Sent: Monday, January 10, 2011 13:24
> To: Lundqvist Petrus (Nokia-MS/Helsinki)
> Cc: Alpert Alan (Nokia-MS-Qt/Brisbane); qt-qml at qt.nokia.com
> Subject: Re: [Qt-qml] Timer without animation ?
>
>> I don't think you really answered the question though. The original question is why the Timer element only works in Item but not with QtObject -based parents.
>
> Hmm, there must be some kind of misunderstanding here... I gave you an
> in-depth explanation of why putting a Timer element inside a QtObject
> doesn't work if you try to do it the standard way, and how to make it
> work.
>
> I even went as far as testing the code on my machine before answering
> to your mail.
>
> Have you read my original mail ?
>
>
> On Mon, Jan 10, 2011 at 6:14 PM,  <petrus.lundqvist at nokia.com> wrote:
>> I don't think you really answered the question though. The original question is why the Timer element only works in Item but not with QtObject -based parents. It's a perfectly valid question as you might use QtObject -based parents for various things and want to have timers in them. Just because QML is for UI's doesn't mean that there aren't non-visual elements as part of the big picture. Models and model items are good examples of this.
>>
>> Peppe
>>
>> ________________________________________
>> From: qt-qml-bounces+petrus.lundqvist=nokia.com at qt.nokia.com [qt-qml-bounces+petrus.lundqvist=nokia.com at qt.nokia.com] on behalf of ext Gregory Schlomoff [gregory.schlomoff at gmail.com]
>> Sent: Monday, January 10, 2011 12:56
>> To: Alpert Alan (Nokia-MS-Qt/Brisbane)
>> Cc: qt-qml at qt.nokia.com
>> Subject: Re: [Qt-qml] Timer without animation ?
>>
>> Edit:  my answer was based on the fact that you wanted to use QtQuick,
>> but for some unknown reason, didn't want to use visual elements (ie:
>> elements inherited from Item).
>>
>> If you want a real GUI-less approach, then Alan's suggestion is the
>> way to go: create your own set of QObject-derived classes, and use
>> them from QML, and do not use the QtQuick module.
>>
>>
>> On Mon, Jan 10, 2011 at 5:47 PM, Alan Alpert <alan.alpert at nokia.com> wrote:
>>> On Monday, January 10, 2011 08:05:26 pm ext Attila Csipa wrote:
>>>> Is it possible to have a Timer without having a visual item that brings in
>>>> the animation timer ? Think
>>>>
>>>> QtObject {
>>>>     Component.onCompleted: console.log("hello world");
>>>>     Timer {
>>>>              interval: 500; running: true; repeat: true;
>>>>              onTriggered: console.log("tick");
>>>>     }
>>>> }
>>>>
>>>> This gives somewhat cryptic Component is not ready error, but does work if
>>>> I replace QtObject with Item. Now, you will be asking well, why don't you
>>>> just use Item, and the answer is that it feels a bit weird - I'm
>>>> experimenting with GUIless QML (think custom structured QtObject creation,
>>>> textual UIs, etc).
>>>>
>>>
>>> Timer is part of the QtQuick module, and that entire module is designed for
>>> GUI creation. If you don't want a GUI, but want to use QML, then just don't
>>> import the QtQuick module and use items from elsewhere. An example of this is
>>> that .qmlproject files import the QmlProject module to store the project
>>> description in QML, entirely GUI-less.
>>>
>>> Since non-GUI modules are not common yet, this means that you may have to
>>> write your own Timer element which does not synchronize with the animation
>>> Timer.
>>>
>>> --
>>> Alan Alpert
>>> Software Engineer
>>> Nokia, Qt Development Frameworks
>>> _______________________________________________
>>> Qt-qml mailing list
>>> Qt-qml at qt.nokia.com
>>> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
>>>
>> _______________________________________________
>> Qt-qml mailing list
>> Qt-qml at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
>>
>


More information about the Qt-qml mailing list