Save QTableWidget
-
wrote on 29 Jan 2013, 14:58 last edited by A Former User
Hi all,
I have built a QTableWidget that the user fills up with numbers. Then it is send via usb 2.0. to a microcontroler.
I already asked a question here on this "forum":http://qt-project.org/forums/viewthread/23923/#111129 to help me with QtableWidget. It worked perfectly.I am now facing another problem... I cannot find how to SAVE the table. It is unaceptable to be unable to save.
Could you point me in the right direction. I will continue trying to find an example in the meantime!
Thanks
-
wrote on 29 Jan 2013, 15:02 last edited by
A QTableWidget is essentiall a QTableView with a default, simple, model.
When you say 'save' you likely mean you want to export the data from this standard model.There are two approaches; the hard one is to write your own model using a datastructure you can implement any way you want.
The suggested, easier solution is to figure out how the default model works and get your data out like that.
See the model() method on the widget, and use the data() method on the QAbstractItemModel.Have fun!
-
wrote on 29 Jan 2013, 15:54 last edited by
Thanks
Here is what I did.. it works pretty well@void MainWindow::saveFile(const QString &name)
{
QFile file(name);
/if (file.open(QIODevice::WriteOnly|QIODevice::Text)) {
file.write(ui->textEdit->toPlainText().toUtf8());
statusBar()->showMessage(tr("File saved successfully."), 3000);
}/if (file.open(QFile::WriteOnly | QFile::Truncate)) { QTextStream data( &file ); QStringList strList; for( int c = 0; c < ui->tableWidget->columnCount(); ++c ) { strList << "\" " + ui->tableWidget->horizontalHeaderItem(c)->data(Qt::DisplayRole).toString() + "\" "; } strList << "\" " + ui->textEdit->toPlainText().toUtf8() + "\" "; data << strList.join(";") << "\n"; for( int r = 0; r < ui->tableWidget->rowCount(); ++r ) { strList.clear(); for( int c = 0; c < ui->tableWidget->columnCount(); ++c ) { QTableWidgetItem* item = ui->tableWidget->item(r,c); //Load items if (!item || item->text().isEmpty()) //Test if there is something at item(r,c) { ui->tableWidget->setItem(r,c,new QTableWidgetItem("0"));//IF there is nothing write 0 } strList << "\" "+ui->tableWidget->item( r, c )->text()+"\" "; } data << strList.join( ";" )+"\n"; } statusBar()->showMessage(tr("File saved successfully."), 3000); file.close(); }
}@
-
wrote on 29 Jan 2013, 15:54 last edited by
Now I need to LOAD files!!
-
wrote on 29 Jan 2013, 16:23 last edited by
I tried this for loading a .csv file. But I get an exception at line 32. What is the problem?
@void MainWindow::loadFile()
{
//QString filename = QFileDialog::getOpenFileName(this);
//QFile file(filename);
/* if (file.open(QIODevice::ReadOnly|QIODevice::Text)) {
ui->textEdit->setPlainText(QString::fromUtf8(file.readAll()));
mFilePath = filename;
statusBar()->showMessage(tr("File successfully loaded."), 3000);
}/
QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), NULL, ("csv File(.csv)"));
QString data;
QFile importedCSV(fileName);
QStringList rowOfData;
QStringList rowData;
data.clear();
rowOfData.clear();
rowData.clear();if (importedCSV.open(QFile::ReadOnly)) { data = importedCSV.readAll(); rowOfData = data.split("\n"); //Value on each row importedCSV.close(); } for (int x = 0; x < rowOfData.size(); x++) //rowOfData.size() gives the number of row { rowData = rowOfData.at(x).split(";"); //Number of collumn int r=rowData.size(); for (int y = 0; y < rowData.size(); y++) { ui->tableWidget->item(x,y)->setText(rowData[y]); } } statusBar()->showMessage(tr("File successfully loaded."), 3000);
}@
-
wrote on 29 Jan 2013, 16:35 last edited by
I have change line 32 with
@ui->tableWidget->setItem(x-1,y,new QTableWidgetItem(rowData[y]));@
But the table is filling up with '' Text '' how do remove ''??
-
wrote on 29 Jan 2013, 18:35 last edited by
Ok I found out how.. thanks. Solved
Note: I had to replace
@"" " + ui->tableWidget->horizontalHeaderItem(c)->data(Qt::DisplayRole).toString() + "" ";@
with
@ui->tableWidget->horizontalHeaderItem(c)->data(Qt::DisplayRole).toString() +
" ";@ -
wrote on 19 Nov 2014, 10:02 last edited by
excuse me, I know it's been a looong while, but could you tell me what ui->textEdit is? I'm having trouble saving my qtablewidget and this isthe only actual solution I can find, but I don't quite understand it
-
Hi,
ui implies that this is a Designer based widget. textEdit is likely to be a QTextEdit
Hope it helps
-
wrote on 12 May 2017, 06:45 last edited by VRonin 5 Dec 2017, 07:45
Hi
I use codefor( int r = 0; r < ui->tableWidget->rowCount(); ++r ) { strList.clear(); for( int c = 0; c < ui->tableWidget->columnCount(); ++c ) { QTableWidgetItem* item = ui->tableWidget->item(r,c); if (!item || item->text().isEmpty()) { ui->tableWidget->setItem(r,c,new QTableWidgetItem("0")); } strList << "\" "+ui->tableWidget->item( r, c )->text()+"\" "; } data << strList.join( ";" )+"\n"; }
I have problem When I read(Open) file and write(save) file replace again (around 3 round).
But file has empty line.
Please help me . -
Hi
I use codefor( int r = 0; r < ui->tableWidget->rowCount(); ++r ) { strList.clear(); for( int c = 0; c < ui->tableWidget->columnCount(); ++c ) { QTableWidgetItem* item = ui->tableWidget->item(r,c); if (!item || item->text().isEmpty()) { ui->tableWidget->setItem(r,c,new QTableWidgetItem("0")); } strList << "\" "+ui->tableWidget->item( r, c )->text()+"\" "; } data << strList.join( ";" )+"\n"; }
I have problem When I read(Open) file and write(save) file replace again (around 3 round).
But file has empty line.
Please help me .Lifetime Qt Championwrote on 12 May 2017, 06:52 last edited by mrjj 5 Dec 2017, 06:52@hannao
Hi and welcome
so you save all rows as
col;col;col ?So what is not working?
There is empty line ?
or nothing at all in file ? -
wrote on 12 May 2017, 06:55 last edited by
Yes, when I save again agin again. This file is empty
-
@hannao
can you show the load code ? -
wrote on 12 May 2017, 07:34 last edited by VRonin 5 Dec 2017, 07:56
I implemented something I consider relatively satisfying (even though there's a lot of room for improvement) here: https://github.com/VSRonin/Qt-Model-Serialisation/tree/dev (make sure you use the dev branch)
CsvModelSerialiser serial; //create the serialiser serial->setModel(tableWidget->model()); // set the model to save serial->setCsvSeparator(";"); // use ; as separator QFile tempFile("TestSave"); // prepare the file to save if (!serial->saveModel(&tempFile)) // save the model to file Q_ASSERT(false);
to load it:
CsvModelSerialiser serial; //create the serialiser serial->setModel(tableWidget->model()); // set the model to load serial->setCsvSeparator(";"); // use ; as separator QFile tempFile("TestSave"); // prepare the file to load if (!serial->loadModel(&tempFile)) // save the model to file Q_ASSERT(false);
-
wrote on 12 May 2017, 07:34 last edited by VRonin 5 Dec 2017, 07:45
Here I use.
void Finalline2::on_openButton_clicked() { QString filename = QFileDialog::getOpenFileName(this,tr("Open Files"),"/home/pi/share/L2/Final Line",tr("Txt Files(*.txt)")); QFile file(filename); QStringList listA; int row=0; if(file.open(QIODevice::ReadOnly)){ while(!file.atEnd()){ QString line = file.readLine(); listA = line.split(","); ui->tableWidget->setColumnCount(listA.size()); ui->tableWidget->insertRow(row); for(int x = 0; x < listA.size() ; x++) { QTableWidgetItem *test = new QTableWidgetItem(listA.at(x)); ui->tableWidget->setItem(row, x, test); } row++; } } file.close(); }
-
Here I use.
void Finalline2::on_openButton_clicked() { QString filename = QFileDialog::getOpenFileName(this,tr("Open Files"),"/home/pi/share/L2/Final Line",tr("Txt Files(*.txt)")); QFile file(filename); QStringList listA; int row=0; if(file.open(QIODevice::ReadOnly)){ while(!file.atEnd()){ QString line = file.readLine(); listA = line.split(","); ui->tableWidget->setColumnCount(listA.size()); ui->tableWidget->insertRow(row); for(int x = 0; x < listA.size() ; x++) { QTableWidgetItem *test = new QTableWidgetItem(listA.at(x)); ui->tableWidget->setItem(row, x, test); } row++; } } file.close(); }
wrote on 12 May 2017, 08:01 last edited by@hannao said in [SOLVED] Save QTableWidget:
listA = line.split(",");
@hannao said in [SOLVED] Save QTableWidget:
data << strList.join( ";" )+"\n";
I see something weird going on here
@hannao said in [SOLVED] Save QTableWidget:
if(file.open(QIODevice::ReadOnly)){
If the file is text you should use the text flag
if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
-
@hannao said in [SOLVED] Save QTableWidget:
listA = line.split(",");
@hannao said in [SOLVED] Save QTableWidget:
data << strList.join( ";" )+"\n";
I see something weird going on here
@hannao said in [SOLVED] Save QTableWidget:
if(file.open(QIODevice::ReadOnly)){
If the file is text you should use the text flag
if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
-
wrote on 12 May 2017, 08:28 last edited by VRonin 5 Dec 2017, 08:36
@hannao said in [SOLVED] Save QTableWidget:
I use listA = line.split(" , ");
It's still different from the save code
Did you try using my code?
You just have to copy everything excluding
model_serialisation_global.h
into your folder and addDEFINES += QT_MODEL_SERIALISATION_EXPORT
to your pro file (I know this is not ideal but if it's still in the dev branch there are reasons) -
@hannao said in [SOLVED] Save QTableWidget:
I use listA = line.split(" , ");
It's still different from the save code
Did you try using my code?
You just have to copy everything excluding
model_serialisation_global.h
into your folder and addDEFINES += QT_MODEL_SERIALISATION_EXPORT
to your pro file (I know this is not ideal but if it's still in the dev branch there are reasons) -
wrote on 25 May 2017, 07:19 last edited by
No, do not bother building the library (in fact I don't think it will work as I miss the .pro file altogether). Just copy everything excluding
model_serialisation_global.h
in your project and add the sources to your project.