Only the last result value of the query is output
-
Hello, I am inevitably using QTableWidget, but I am troubled that only one result value of the query is printed continuously.
How can I print out all the values?
Also, I can't use the sqlQuery model for QTableWidget, so is there a way to use it?
Post the code and pictures of the results below.
Also, please understand that the translation is not smooth.
void MainWindow::reporttable(){ QString TIME = ui->selectdateEdit_2->text(); QString CLOCK = ui->selectdateEdit_3->text(); QString box = ui->comboBox_Select_2->currentText(); QSqlQuery query; QString sql; sql = QString("select max(case when t_block='JB001' then T_FlOW1 end) as JB001, max(case when t_block='JB002' then T_FlOW1 end) as JB002 from ulsansstbl WHERE T_DATE BETWEEN '%1 00:00:00' AND '%2 23:59:59' AND t_block like '%3%' group by t_date order by t_date").arg(TIME).arg(CLOCK).arg(box); if(!query.exec(sql)){ qDebug () <<"query error:" << query.lastError(); } else { qDebug () <<"query access:" << query.lastQuery(); int li = 7; QString str; while(query.next()) { for(int i = 0; i != li; i+=1){ ui->tableWidget->setItem(i+5,1,new QTableWidgetItem(str)); } ui->tableWidget->setRowCount(li); str = query.value(0).toString(); qDebug() << "value:" << str; } } }
This is what I'm developing on the Qtablewidget.
This is the value of the data on the oracle DB.
This is the value displayed in the qt internal Debug. As you can see, the value is normally printed.
However, the QTablewidget does not display two values, and only one is output.
-
The logic you are using to fill the table appears to be broken.
sql = QString("select max(case when t_block='JB001' then T_FlOW1 end) as JB001, max(case when t_block='JB002' then T_FlOW1 end) as JB002 from ulsansstbl WHERE T_DATE BETWEEN '%1 00:00:00' AND '%2 23:59:59' AND t_block like '%3%' group by t_date order by t_date").arg(TIME).arg(CLOCK).arg(box);
Returns as many rows as there are with distinct t_date values between
TIME
andCLOCK
. This might be zero or more rows.
BTW: You really should use bind variables not string substitution.if(!query.exec(sql)){ qDebug () <<"query error:" << query.lastError(); } else { qDebug () <<"query access:" << query.lastQuery(); int li = 7; QString str;
How many rows does the table widget have at this point? You set it to
li
later, but it might be anything here.
Thestr
variable is a null string here and you use it a few lines down without ever setting it.while(query.next()) { for(int i = 0; i != li; i+=1){ ui->tableWidget->setItem(i+5,1,new QTableWidgetItem(str)); }
You put the same value into column 1 of rows 5 to 11 ( i.e. 0+5 to 6+5) of the table widget. This seems unusual, but only you know what your table looks like.
The first time through the while loop
str
will be a null string.
The second and later loops,str
will contain the value from the previous result record.
You will never insert the value from the last result record.ui->tableWidget->setRowCount(li);
Now you set the table to have
li
(7) rows. Even if there were previously rows 7 to 11, they are gone now.
Why set this fixed row count on every result record?str = query.value(0).toString();
Only now do you fetch the value from the result record into
str
. This should happen earlier.qDebug() << "value:" << str; } // end of while
-
Hi
You seem add all data to col 1
Have you seen the docs ?
https://doc.qt.io/qt-6/qsqlquery.htmlYou can use
query.value(0).toString();
to get data for col 1 and so on.But if all you wish is to load the data into a table as 1:1 mapping, you can get this "code free" if you use
https://doc.qt.io/qt-6/qsqlquerymodel.html
and a QTableView.
You simply use the query of the model and the view will just display the data.ahh. ChrisW67 was faster :)
-
Hi
You seem add all data to col 1
Have you seen the docs ?
https://doc.qt.io/qt-6/qsqlquery.htmlYou can use
query.value(0).toString();
to get data for col 1 and so on.But if all you wish is to load the data into a table as 1:1 mapping, you can get this "code free" if you use
https://doc.qt.io/qt-6/qsqlquerymodel.html
and a QTableView.
You simply use the query of the model and the view will just display the data.ahh. ChrisW67 was faster :)