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