Qtmobility Organizer Qml And C++
-
Hi,
I defined in my qml
@
property OrganizerModel organizer:OrganizerModel{
id: organizer
manager:"qtorganizer:memory:id=qml"
startPeriod:"2012-02-21"
endPeriod:"2012-02-23"
autoUpdate:true
}
@after i call
@
onClicked: {calendarManager.createEvent(new Date(), new Date(), "dum"); organizer.update(); console.log("N° item from qml" + organizer.itemCount + " from c++ " + calendarManager.count()); }
@
and i see N° item from qml 0 from c++ 1
in c++ i have
@
CalendarManager::CalendarManager(QObject *parent) :
QObject(parent)
{
_manager = QOrganizerManager::fromUri("qtorganizer:memory:id=qml");
}void CalendarManager::createEvent(QDateTime startTime, QDateTime endTime, QString description)
{
QOrganizerEvent event;
event.setStartDateTime(startTime);
event.setDescription(description);if (_manager->saveItem(&event) ) qDebug() << "save\n"; else qDebug() << "error " << _manager->error() << endl;
}
int CalendarManager::count()
{
QDateTime last(QDate::currentDate());
QDateTime today(QDate::currentDate());
last = last.addDays(2);return _manager->items(today, last).length();
}
@My question is: why in qml i don't see new item in organizer model?
-
Maybe you should reload the model? Im not sure, i didnt used the QTM calendar :)
-
In short, the C++ QOrganizerManager provides synchronous APIs, while the C++ QOrganizerAbstractRequest and the QML OrganizerModel provide asynchronous APIs. Once the model gets updated, the modelChanged signal will be emitted.
-
i force update with organizer.update(); but nothing
-
update() is also async, and if you have autoUpdate set to true, you don't need to call update().
-
ok, i don't understand, what's a solution?
can i create Event in qml? I think no, property is all read-only -
You can create on event like this:
@Event {
id: myEvent
startDateTime: "2012-02-23T00:00:00Z"
endDateTime: "2012-02-23T08:00:00Z"
displayLabel: "my event"
}@Then saves it:
@organizerModel.saveItem(myEvent);@ -
yes, but it's true for static event, after can't change a date, ecc.
-
You can. And you only need to wait until the model is ready, i.e. the modelChanged() signal is emitted.
The reason why your code (as shown below) doesn't work is that, the update() function is async, and it returns immediately, so when you call the console.log() function, the model is NOT updated yet.
@organizer.update();
console.log("N° item from qml" + organizer.itemCount + " from c++ " + calendarManager.count());@ -
Excellent to see interested people around these APIs :)
Other possible problem with original implementation is, that you might be having two separate managers. One created in Qml and one in C++. Memory backend is not really intelligent and does not share same data storage with multiple manager instances.
But as xizzhu was already pointing out, you don't actually even need to access C++ model, you can create and store events with plain Qml. You can utilise same static event for saving multiple different events to memory or you can create events dynamically.