PDA

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



twizzle91
27th October 2010, 19:43
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!

wysota
27th October 2010, 19:57
QFile, I guess.

twizzle91
9th November 2010, 20:06
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 :/

wysota
9th November 2010, 21:05
but it still doesn't save the contents of the listbox :/
What did you try so far to save the contents?

twizzle91
16th November 2010, 20:25
so far i have:


void listBoxx::saveFile()
{
QString filename = QFileDialog::getSaveFileName(this, tr("Save List"), "", tr("Text Files (*.txt);;All Files (*)"));

if (filename.isEmpty())
return;
else {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("cannot open file"), file.errorString());
return;
}

QTextStream m_textEdit(&file);
m_textEdit << m_newList;

QDataStream stream(&file);
stream << m_newList;

file.close();

}

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.

wysota
16th November 2010, 22:38
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?

janorcutt
17th November 2010, 18:27
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.