Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: Populate Listview from Database

  1. #21
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Populate Listview from Database

    Quote Originally Posted by arcade View Post
    yes i want to add it in the same model !!
    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,
    _

  2. #22
    Join Date
    Jul 2016
    Posts
    26
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Populate Listview from Database

    Quote Originally Posted by anda_skoa View Post
    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

    Qt Code:
    1. if(!db.open())
    2. {
    3. qDebug() <<"error in opening DB";
    4. }
    5. else
    6. {
    7. qDebug() <<"connected to DB" ;
    8.  
    9. }
    10.  
    11. QVector<QString> page_id;
    12. QVector<QString> page_title;
    13.  
    14. QSqlQuery quer("SELECT page_ID , page_title FROM Pages");
    15. while (quer.next()) {
    16. QString i = quer.value(0).toString();
    17. page_id.push_back(i);
    18. QString p = quer.value(1).toString();
    19. page_title.push_back(p);
    20.  
    21.  
    22. }
    23. for (int i = 0; i < page_id.size(); ++i)
    24. {
    25. qDebug() << page_id.at(i).toLocal8Bit().constData() << endl;
    26. qDebug() << page_title.at(i).toLocal8Bit().constData() << endl;
    27. mod.addpages(list(page_title.at(i) , page_id.at(i)));
    28.  
    29.  
    30.  
    31.  
    32. }
    33.  
    34.  
    35.  
    36.  
    37.  
    38. QQmlContext *ctxt = engine.rootContext();
    39. ctxt->setContextProperty("myModel", &mod);
    To copy to clipboard, switch view to plain text mode 

    2. now this is how dbmanager.cpp calls the addpages()

    Qt Code:
    1. listmodel mod ; // declared on global scope
    2.  
    3. 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 ?

  3. #23
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Populate Listview from Database

    Quote Originally Posted by arcade View Post
    so i need to pass the same instance in main.cpp to dbmanager right ?? Can i use pointers for that ?
    Yes.
    Or you do the initial load also in dbmanager.

    Btw, your loading loop doesn't need local vectors, you can directly call addpages

    Qt Code:
    1. while (quer.next()) {
    2. QString i = quer.value(0).toString();
    3. QString p = quer.value(1).toString();
    4. mod.addpages(p, i);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by arcade View Post
    Qt Code:
    1. mod.addpages(list(page_title,QString(pageid)));
    To copy to clipboard, switch view to plain text mode 
    You also might want to use QString::number(pageid), assuming pageid is a numerical value and you want that formatted into a string.

    Cheers,
    _

  4. #24
    Join Date
    Jul 2016
    Posts
    26
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Thumbs up Re: Populate Listview from Database

    Thanks !! It's done

Similar Threads

  1. Replies: 2
    Last Post: 17th May 2016, 00:50
  2. Replies: 24
    Last Post: 14th March 2016, 09:58
  3. Replies: 0
    Last Post: 16th October 2015, 17:51
  4. Populate QTreeView from database
    By yagabey in forum Qt Programming
    Replies: 15
    Last Post: 26th March 2015, 03:10
  5. Replies: 2
    Last Post: 26th March 2015, 02:56

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.