I have a Qt tableView that loads the data from the SQLite database and I have configured it in such a way that in the default view, the first row is automatically selected and I can execute a query on that row by pressing a button-'Present'. Now, I want my program to automatically select the next row after I pressed the 'button' for the first time so that the when I press 'Present' for the second time, the query is executed on the second row. So, basically, I want to change the selection of row whenever a button is pressed until the end of row numbers is reached.
I have searched quite a few sites for the solution, but I could not get the one for my problem.
Code for viewing table s_info and selecting the first row as default.
void table::on_view_clicked()
{
MainWindow conn;
QSqlQueryModel * modal = new QSqlQueryModel();
conn.connOpen();
QSqlQuery* qry= new QSqlQuery(conn.info);
qry->prepare("Select Name,Roll_No from s_info order by Roll_no");
qry->exec();
modal->setQuery(*qry);
ui-> tableView ->setModel(modal);
ui->tableView-> setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->selectRow(0);
ui->tableView->setFocus();
conn.connClose();
qDebug()<< (modal->rowCount());
}
Code for the execution of a query when a button name 'Present' is clicked. Note that I have performed query execution based on Column Roll_No in my s_info table and index of Roll No for the 1st row is(0,1)
void table::on_present_clicked()
{
QAbstractItemModel *model = ui->tableView->model();
QModelIndex index = model->index(0,1);
QString roll= (index.data().toString());
MainWindow conn;
conn.connOpen();
QSqlQuery qry;
QSqlTableModel modal;
qry.prepare("Update s_info set Present_Days=Present_Days + 1 where
Roll_No='"+roll+"'");
qry.exec();
conn.connClose();
}
I expect that when I click a Present for the second time, the row selection is shifted to the second row and the query is executed on that row. I want this to occur until I reached the end of the number of rows.
The following example illustrates what you are going to achieve. The key-point is that you are going to need an QItemSelectionModel
, which manages you selection. Very often one forgets to explicitly set the model of the QItemSelectionModel
being the model of the view.
Now, if you will select one row in an table view the next button will select the next row. Selecting the next row basically means to select all columns in the next row.
It shouldn't matter if you are using something like an QSqlTableModel
the usage should be simply the same.
#include <QApplication>
#include <QTableView>
#include <QPushButton>
#include <QHBoxLayout>
#include <QStandardItemModel>
#include <QItemSelectionModel>
int main(int argc, char** args) {
QApplication app(argc, args);
auto frame = new QFrame;
frame->setLayout(new QHBoxLayout);
auto tableView = new QTableView;
tableView->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
tableView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
auto model = new QStandardItemModel;
tableView->setModel(model);
auto selectionModel = new QItemSelectionModel;
selectionModel->setModel(model);
tableView->setSelectionModel(selectionModel);
frame->layout()->addWidget(tableView);
auto button = new QPushButton("Next");
frame->layout()->addWidget(button);
model->setRowCount(10);
model->setColumnCount(10);
frame->show();
QObject::connect(button, &QPushButton::clicked, [&]()
{
auto indices = selectionModel->selectedIndexes();
if (indices.isEmpty()) return;
QModelIndexList empty;
selectionModel->select(QModelIndex(), QItemSelectionModel::SelectionFlag::Clear);
for (auto index : indices)
{
auto newIndex=index.sibling(index.row() + 1, index.column());
selectionModel->select(newIndex,QItemSelectionModel::SelectionFlag::Select);
}
});
app.exec();
}