Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当按下按钮时,如何在Qt tableView中自动选择下一行?_C++_Sql_Qt_Qt5_Qtableview - Fatal编程技术网

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

C++ 当按下按钮时,如何在Qt tableView中自动选择下一行?,c++,sql,qt,qt5,qtableview,C++,Sql,Qt,Qt5,Qtableview,我有一个Qt tableView,它从SQLite数据库加载数据,并且我以这样一种方式配置它:在默认视图中,自动选择第一行,我可以通过按下按钮“Present”对该行执行查询。现在,我希望我的程序在我第一次按下“按钮”后自动选择下一行,这样当我第二次按下“显示”时,查询将在第二行执行。因此,基本上,我想在按下按钮直到到达行号末尾时更改行的选择 我已经搜索了好几个网站来寻找解决方案,但我没有找到解决我问题的网站 查看表格s_信息并选择第一行作为默认行的代码 void table::on_view_

我有一个Qt tableView,它从SQLite数据库加载数据,并且我以这样一种方式配置它:在默认视图中,自动选择第一行,我可以通过按下按钮“Present”对该行执行查询。现在,我希望我的程序在我第一次按下“按钮”后自动选择下一行,这样当我第二次按下“显示”时,查询将在第二行执行。因此,基本上,我想在按下按钮直到到达行号末尾时更改行的选择

我已经搜索了好几个网站来寻找解决方案,但我没有找到解决我问题的网站

查看表格s_信息并选择第一行作为默认行的代码

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

我希望当我第二次单击一个礼物时,行选择被转移到第二行,并且在该行上执行查询。我希望这种情况一直持续到行数的末尾。

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

现在,如果要在表视图中选择一行,则“下一步”按钮将选择下一行。选择下一行基本上意味着选择下一行中的所有列

如果您使用的是类似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();
}
#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();
}