【问题标题】:How to select next row automatically in Qt tableView whenever a pushbutton is pressed?每当按下按钮时,如何在 Qt tableView 中自动选择下一行?
【发布时间】:2019-07-13 05:44:57
【问题描述】:

我有一个 Qt tableView,它从 SQLite 数据库 加载数据,并且我已将其配置为在默认视图中,first行被自动选择,我可以在该行上执行查询,方法是按按钮-'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 时,行选择会转移到第二行并在该行上执行查询。我希望这种情况一直发生,直到我达到行数的末尾。

【问题讨论】:

    标签: c++ sql qt qt5 qtableview


    【解决方案1】:

    以下示例说明了您将要实现的目标。关键是您将需要一个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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-29
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多