10-03-2012 12:00 PM
Hi,
I am having trouble inserting data from a QVariantList into a ListView using a GroupDataModel. The app works, but the list is empty. I have confirmed that the QVariantList is not empty. I have been struggling with this for some time and would appreciate any help.
From the app constructor :
mDb = new databaseHandler();
mQml = QmlDocument::create("asset:///main.qml");
//-- setContextProperty expose C++ object in QML as an variable
if (!mQml->hasErrors()) {
mQml->setContextProperty("app", this);
mTimer = new QTimer(this);
connect(mTimer, SIGNAL(timeout()), this, SLOT(update()));
mRoot = mQml->createRootObject<AbstractPane>();
loadData();
Application::instance()->setScene(mRoot);
//int i = 9;
}
else
{
QList<QDeclarativeError> error = mQml->errors();
}
void App::loadData()
{
QString databaseName = "timetracker_test_data.db";
mTimeEntries = mDb->loadDataBase(databaseName);
GroupDataModel *mDataModel = mRoot->findChild<GroupDataModel*>("timeEntryModel"
QVariantList sqlData;
for(int count = 0; count < mTimeEntries.count(); count ++)
{
QVariantMap timeEntryMap;
timeEntryMap["description"] = mTimeEntries[count].getDescription();
int time = mTimeEntries[count].getTotalTime();
timeEntryMap["time"] = intTimeToString(time);
sqlData.append(timeEntryMap);
//mRoot->setProperty("currTimeText", mTimeEntries[count].getDescription());
mDataModel->insert(timeEntryMap);
}
mDataModel->insertList(sqlData);
}
From main.qml :
ListView {
id: listContainer
property variant itemTextStyle: defaultBlackTextStyle
dataModel: timeEntryModel
listItemComponents: [
ListItemComponent {
type: "TimeEntry"
TimeEntry {
}
}
]
attachedObjects: [
// A GroupDataModel is used for presenting the SQL data. All set up is done here
// except for the actual population of the model, which is done in C++.
GroupDataModel {
id: timeEntryModel
objectName: "timeEntryModel"
}
]
}
and from TimeEntry.qml :
Container {
layout: StackLayout {
}
Label {
// The title is bound to the data in models/recipemodel.xml title attribute.
text: ListItemData.description;
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.Black
}
}
Label {
// The title is bound to the data in models/recipemodel.xml title attribute.
text: ListItemData.time;
rightMargin: 25.0
leftMargin: 25.0
layoutProperties: StackLayoutProperties {
}
rotationZ: 0.0
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.Black
}
}
}
Solved! Go to Solution.
10-04-2012 02:41 AM
Try inserting data like this:
QVariantList qvList = mTimeEntries.toList();
for (QList<QVariant>::iterator it = qvList.begin(); it != qvList.end();
it++) {
mDataModel->insert(it->toMap());
}
& access data in qml as follows:
text: ListItemData.SQLFieldName
Hope it helps.
Regards,
Nishant Shah
10-04-2012 10:53 AM
Thanks Nishant,
Unfortunately this approach does not solve the problem and the list is still empty.
10-06-2012 11:50 PM
Try changing the list item type to "item", or simply removing the list item type entirely. The type parameter is only useful if you're explicitly customizing the list item type mapping, and causes problems otherwise.
10-08-2012 05:21 PM
Your problem is that you are not overriding the itemType function in the listview qml so the listview doesnt know which component to give to each item.
function itemType(data, indexPath) { } or something like that, the actual method is in the docs somewhere
10-15-2012 09:11 PM
Thanks for the replies on this one all. Turns out I was diagnosing the incorrect issue all along. Turns out my listview was being populated from the beginning but a UI issue was causing my listview to be hidden by another component. (don't ask why I never checked this at first) The nice thing about this "non-issue" is that I now know how the listview works inside and out as I tried every conceivable method to solve this![]()
Gerry