QTableView export to .csv The number of selected rows is limited to only 256

I wrote a gui program that will connect to the oracle db to retrieve data after entering the request. the extracted data is displayed in the widgets of the QTableView table model. and later, the result of the QTableView is exported to a CSV file.

QString MyQuery = ui->lineQuery->text(); db.open(); QSqlQuery query(MyQuery,db); if(query.exec()) { qDebug()<<QDateTime::currentDateTime()<<"QUERY SUCCESS "; ui->queryButton->setStyleSheet("QPushButton {background-color: rgb(0, 255, 0);}"); this->model=new QSqlQueryModel(); model->setQuery(MyQuery); ui->tableViewOra->setModel(model); QString textData; int rows=model->rowCount(); int columns=model->columnCount(); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { textData += model->data(model->index(i,j)).toString(); textData += ", "; // for .csv file format } textData += "\n"; // (optional: for new line segmentation) } QFile csvfile("/home/aj/ora_exported.csv"); if(csvfile.open(QIODevice::WriteOnly|QIODevice::Truncate)) { QTextStream out(&csvfile); out<<textData; } csvfile.close(); } 

now the problem is that during the request, I noticed that there are 543 rows in the QTableView (which is the right reason because there are 543 entries). But when the .csv file is exported, only 256 lines.

Is there a size limit that I don't know about ???

What variables should I take care if I want to export .csv files up to 1000 lines long? thanks.

+2
source share
1 answer

I think that when you first read model->rowCount() , the model did not fully take all the results. Although it will be displayed later when the table view is displayed, which will lead to the full display of the rows in the table view.

Try using QSqlQueryModel::fetchMore before reading the row counter:

 while (model->canFetchMore()) model->fetchMore(); int rows=model->rowCount(); int columns=model->columnCount(); 

[Additional Information from Tay2510]:

You can simply change the file open flag

 if(csvfile.open(QIODevice::WriteOnly|QIODevice::Truncate)) 

to

 if(csvfile.open(QIODevice::WriteOnly)) 

The former will overwrite the same file, while the latter will add data to it.

+4
source

Source: https://habr.com/ru/post/1204672/


All Articles