Results 1 to 7 of 7

Thread: made QListBox, now how do I save what's in the box to a file?

  1. #1
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default made QListBox, now how do I save what's in the box to a file?

    hello,

    I've created a QListBox that when you press a button (insert) the text from the QLineEdit is entered into the Listbox. But now i need to make a new button (which i've already done) and have it so when that button is pressed, the contents of the listbox will be saved to a file somewhere.

    I guess my major question is where do I start? what Classes should I be looking at to figure out this problem??

    thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: made QListBox, now how do I save what's in the box to a file?

    QFile, I guess.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: made QListBox, now how do I save what's in the box to a file?

    well I've gotten to the point where i can save a .txt file to a location... but it still doesn't save the contents of the listbox :/

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: made QListBox, now how do I save what's in the box to a file?

    Quote Originally Posted by twizzle91 View Post
    but it still doesn't save the contents of the listbox :/
    What did you try so far to save the contents?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: made QListBox, now how do I save what's in the box to a file?

    so far i have:

    Qt Code:
    1. void listBoxx::saveFile()
    2. {
    3. QString filename = QFileDialog::getSaveFileName(this, tr("Save List"), "", tr("Text Files (*.txt);;All Files (*)"));
    4.  
    5. if (filename.isEmpty())
    6. return;
    7. else {
    8. QFile file(filename);
    9. if (!file.open(QIODevice::WriteOnly)) {
    10. QMessageBox::information(this, tr("cannot open file"), file.errorString());
    11. return;
    12. }
    13.  
    14. QTextStream m_textEdit(&file);
    15. m_textEdit << m_newList;
    16.  
    17. QDataStream stream(&file);
    18. stream << m_newList;
    19.  
    20. file.close();
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

    and i'm able to save a file to a folder, but then when I click the file to open in, it says "file is of an unknown type". and the contents are still not saved to the file either.
    Last edited by wysota; 16th November 2010 at 22:34. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: made QListBox, now how do I save what's in the box to a file?

    What is m_newList? What is "it" that says that file is of an unknown type? Why are you streaming m_newList (whatever it is) to both a text stream and a binary stream? Do you know what you are doing or are you trying things randomly expecting that Qt was written for the sole purpose so that you can write your program so there just has to be a single call that does what you want?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: made QListBox, now how do I save what's in the box to a file?

    QXmlWriter and QXmlReader may be what your looking for. You can then iterate through the QListBox items and write them to an xml file

    then you can try the process in reverse...
    I would read the documentation on xml and have a look at the examples shipped with creator.

Similar Threads

  1. Not enough space to save file
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2010, 23:14
  2. Save model to file
    By matulik in forum Newbie
    Replies: 4
    Last Post: 11th May 2010, 23:02
  3. How to save an existing file
    By rk0747 in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2010, 10:11
  4. Save Icon in a file
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2009, 18:07
  5. Using QSettings to read pre-made INI file..
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 05:36

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.