
Originally Posted by
anda_skoa
Then you need to call addpages() on that model instance, not create a new instance.
I.e. your code snippet looks like you are creating a new instance called "mod" on which you call addpages().
Cheers,
_
Hello , i understand what you are saying .
My code goes like this .
1. Main.cpp : populate the views
if(!db.open())
{
qDebug() <<"error in opening DB";
}
else
{
qDebug() <<"connected to DB" ;
}
QVector<QString> page_id;
QVector<QString> page_title;
QSqlQuery quer
("SELECT page_ID , page_title FROM Pages");
while (quer.next()) {
QString i
= quer.
value(0).
toString();
page_id.push_back(i);
QString p
= quer.
value(1).
toString();
page_title.push_back(p);
}
for (int i = 0; i < page_id.size(); ++i)
{
qDebug() << page_id.at(i).toLocal8Bit().constData() << endl;
qDebug() << page_title.at(i).toLocal8Bit().constData() << endl;
mod.addpages(list(page_title.at(i) , page_id.at(i)));
}
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", &mod);
if(!db.open())
{
qDebug() <<"error in opening DB";
}
else
{
qDebug() <<"connected to DB" ;
}
QVector<QString> page_id;
QVector<QString> page_title;
QSqlQuery quer("SELECT page_ID , page_title FROM Pages");
while (quer.next()) {
QString i = quer.value(0).toString();
page_id.push_back(i);
QString p = quer.value(1).toString();
page_title.push_back(p);
}
for (int i = 0; i < page_id.size(); ++i)
{
qDebug() << page_id.at(i).toLocal8Bit().constData() << endl;
qDebug() << page_title.at(i).toLocal8Bit().constData() << endl;
mod.addpages(list(page_title.at(i) , page_id.at(i)));
}
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", &mod);
To copy to clipboard, switch view to plain text mode
2. now this is how dbmanager.cpp calls the addpages()
listmodel mod ; // declared on global scope
mod.
addpages(list
(page_title,
QString(pageid
)));
listmodel mod ; // declared on global scope
mod.addpages(list(page_title,QString(pageid)));
To copy to clipboard, switch view to plain text mode
i get the problem i.e. like you said creating new instance of listmodel ( first in main.cpp and other here in dbmanager.cpp )
so i need to pass the same instance in main.cpp to dbmanager right ?? Can i use pointers for that ?
Bookmarks