Reputation: 69
How do i load a text file into QListWidget , such that each line in the textfile is an item in QListWidget,
and once i populate it, i also want to save all items of listwidget into a text file , such that each item of listwidget is in a line in the text file , to accomplish that i am attempting this
QString temp;
for(int i=0; i< ui->mylistwidget->count();i++)
{
QListWidgetItem* item = ui->mylistwidget->item(i);
temp += item->text();
}
std::string total_script = temp.toStdString();
i got all the text of listwidget items in a string , what do i do next if i want to save it in a text file? and also how do i load text file and populate my qlistwidget with items ?
Upvotes: 1
Views: 697
Reputation: 52317
Qt provides QFile and QSaveFile classes for convenient file input/output operations. QSaveFile is special in as it will only (over)write the destination file when you commit it. For both parsing and writing the file contents, you can use QTextStream, which exposes the file contents as a stream you can read from or write to, including conversion of various variable types.
For importing from file:
For exporting to file:
commit()
on the file so it is writtenExample without the loop:
QSaveFile fileOut(filename);
QTextStream out(&fileOut);
out << "Qt rocks!" << Qt::endl; // do this for every item
fileOut.commit()
Upvotes: 1