void XXXX::fetchData()
{
model->removeRows(0, model->rowCount());
QSqlQuery q;
QString strSql = "SELECT col1, col2, col3, col4 FROM table1 "
"WHERE 1 = 1 ";
QString strSqlCnt = "SELECT COUNT(*) FROM table1 "
"WHERE 1 = 1 ";
strSql += " AND col1 LIKE 'xxx%%'";
strSqlCnt += " AND col1 LIKE 'xxx%%'";
if (tableView->isSortingEnabled())
{
switch (tableView->sortSection())
{
case 0:
strSql += " ORDER BY col1";
break;
case 1:
strSql += " ORDER BY col2";
break;
default:
strSql += " ORDER BY col3";
}
if (tableView->sortOrder() == Qt::DescendingOrder)
{
strSql += " DESC";
}
}
else
{
strSql += " ORDER BY col1";
}
int count = 0;
if (tableView->pageRowCount() > 0)
{
strSql += QString(" LIMIT %1 OFFSET %2")
.arg(tableView->pageRowCount())
.arg((tableView->page() - 1)
* tableView->pageRowCount());
q.exec(strSql);
while (q.next())
{
.....
}
q.exec(strSqlCnt);
if (q.next())
{
count = q.value(0).toInt();
}
}
else
{
q.exec(strSql);
while (q.next())
{
......
count++;
}
}
tableView->setPageInfo(tableView->page(), count);