Save QTableWidget
-
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 . -
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);
-
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(); }
@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)){
-
@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)