gpt4 book ai didi

c++ - 每当按下按钮时,如何在 Qt tableView 中自动选择下一行?

转载 作者:行者123 更新时间:2023-11-30 04:45:59 37 4
gpt4 key购买 nike

我有一个 Qt tableView,它从 SQLite 数据库 加载数据,我配置它的方式是在默认 View 中,第一个行被自动选中,我可以通过按按钮-'Present' 对该行执行查询。现在,我希望我的程序在我第一次按下“按钮”后自动选择下一行,这样当我为按下“Present”时>第二次,查询在第二行执行。所以,基本上,我想在按下按钮时更改行的选择,直到到达行号的末尾。

我已经在很多网站上搜索了解决方案,但找不到解决我问题的网站。

查看表 s_info 并选择第一行作为默认行的代码。

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());
}

单击按钮名称“Present”时执行查询的代码。请注意,我已根据s_info 表中的列Roll_No 执行查询执行,第一行的Roll No 索引是( 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();
}

我希望当我第二次单击 Present 时,行选择会转移到第二行,并且会在该行上执行查询。我希望这种情况发生,直到达到行数的末尾。

最佳答案

以下示例说明了您将要实现的目标。关键点是您将需要一个QItemSelectionModel,它管理您的选择。人们常常忘记显式地将 QItemSelectionModel 的模型设置为 View 的模型。

现在,如果您要在表格 View 中选择一行,则下一个按钮将选择下一行。选择下一行基本上意味着选择下一行中的所有列。

如果您使用的是类似QSqlTableModel 的东西,用法应该完全相同。

#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();
}

关于c++ - 每当按下按钮时,如何在 Qt tableView 中自动选择下一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57016509/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com