Hi ,
In my example I use a model/view. My class is QSqlTableModel for the model and QTableView for the view.
I know save data to a file (.txt and even .csv if I wish).
void Data_Example::txtExport()
{
tr("Save"), "",
tr("Save (*.txt);;All Files (*)"));
if (fileName.isEmpty())
return;
else {
{
QMessageBox::information(this, tr
("Unable to open file"),
file.errorString());
return;
}
data ="";
for (int row = 0; row < model->rowCount(); ++row)
{
for (int field = 0; field < record.count(); ++field)
{
if(field != 0 && field !=4 )
{
if (field > 1) data += "\n";
data += record.field(field).value().toString();
}
}
}
output.setCodec("UTF-8");
output << data;
}
void Data_Example::txtExport()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save"), "",
tr("Save (*.txt);;All Files (*)"));
if (fileName.isEmpty())
return;
else {
QFile file(fileName);
if (!file.open((QIODevice::WriteOnly) | QIODevice::Text))
{
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QString data;
data ="";
for (int row = 0; row < model->rowCount(); ++row)
{
QSqlRecord record = model->record(row);
for (int field = 0; field < record.count(); ++field)
{
if(field != 0 && field !=4 )
{
if (field > 1) data += "\n";
data += record.field(field).value().toString();
}
}
}
QTextStream output(&file);
output.setCodec("UTF-8");
output << data;
}
To copy to clipboard, switch view to plain text mode
And in my file (.txt) I have for instance:
Alicia
Tucker
21/09/1985
John
Smith
18/02/1964
Jessica
Houston
07/11/1993
Now, I search to load this file or an other file like this but I don't find solutions...
I start to write this:
void Data_Example::txtImport()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open My File"), "",
tr("My File (*.txt);;All Files (*)"));
if (fileName.isEmpty())
return;
else
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::information(this, tr("Unable to open file"),
file.errorString());
return;
}
QTextStream input(&file);
QString line;
do
{
input >> data;
line = input.readLine();
} while(!line.isNull());
//file.close();
}
}
This part doesn't function because I don't have a loop which load data in my QslqTableModel.
If somebody know...
Thanks.
Bookmarks