Results 1 to 5 of 5

Thread: How to save and load listView row data?

  1. #1
    Join Date
    Jul 2019
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to save and load listView row data?

    I made a listView with add and remove row functions by following this tutorial https://www.bogotobogo.com/Qt/Qt5_QL...elView_MVC.php but without the QComboBox.

    I binded the QStringListModel with an empty QStringList from another .cpp file.

    Whenever I close the app after adding rows, the listView empties. How can I save the ListView rows so that they load when I open the app again?
    Last edited by twentynineteen; 25th July 2019 at 08:29. Reason: updated contents

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,117
    Thanks
    235
    Thanked 656 Times in 646 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to save and load listView row data?

    You could try using QSettings or you could invent your own format to store the list in a file in a known location. You could load it in the constructor for your main window or for your list window, and you can choose to save it any time the list changes or only when the app closes.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2019
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to save and load listView row data?

    After some research, I tried the following to save the listView into a text file:

    Qt Code:
    1. QTextStream stream(&file);
    2. for (int i = 0; i < stringList.size(); ++i)
    3. stream << stringList.at(i) << '\n';
    To copy to clipboard, switch view to plain text mode 

    None of the rows were saved.
    According to this https://stackoverflow.com/questions/...tringlistmodel , apparently it's because QStringListModel only stores a copy of the stringList.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to save and load listView row data?

    Quote Originally Posted by twentynineteen View Post
    None of the rows were saved.
    Was the loop executed as many times as you expected?

    Quote Originally Posted by twentynineteen View Post
    According to this https://stackoverflow.com/questions/...tringlistmodel , apparently it's because QStringListModel only stores a copy of the stringList.
    What do you mean with "only". A copy is obviously independent of anything else, so once the model gets the data it will hold on to it.
    So even if you modify the original list variable, it will still have all the data you added to it.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,117
    Thanks
    235
    Thanked 656 Times in 646 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to save and load listView row data?

    None of the rows were saved.
    Further to anda_skoa's reply, where is the "stringList" variable you are trying to save defined? Is it the same one you use to load the QStringListModel? Did you retrieve it from the model (using QStringListModel::stringList()) prior to executing this code?

    I suspect that if your file is empty, it is because you are serializing an empty string list and not the one held by your model.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. load a specific image for each item of listview
    By Binary01 in forum Qt Quick
    Replies: 4
    Last Post: 19th February 2016, 18:04
  2. Save/Load JSON using QML
    By aIsmail in forum Newbie
    Replies: 1
    Last Post: 16th November 2015, 08:51
  3. Load more items to listview
    By matsukan in forum Qt Quick
    Replies: 1
    Last Post: 14th November 2012, 23:48
  4. Save to SVG and load from it
    By Radagast in forum Qt Programming
    Replies: 3
    Last Post: 14th June 2008, 14:22
  5. Can we save & load the settings in GUI !
    By Krish in forum Newbie
    Replies: 6
    Last Post: 26th March 2008, 15:33

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.