Qt TableView实现翻页,翻页选中,删除,跳转

案列参照飞扬青云大神的,添加了翻页后选中效果,模糊搜索,
选中删除和全部删除。

当前实现翻页选中,并将选中的删除
在这里插入图片描述

选中行和取消行的代码,并将选中情况放进map中去


on_tableView_SelectRows(const QItemSelection &selected, const QItemSelection &deselected)
{
    int selected_rows = -1;
    int deselected_rows = -1;
    int db_index_selected = -1;
    int db_index_deselected = -1;

    foreach(QModelIndex index, selected.indexes())
    {
        qDebug()<<"双击:"<<index;
        //获取数据库的索引
        QSqlRecord record = queryModel->record(index.row());
        db_index_selected = record.value(0).toInt();
        //获取view索引
        selected_rows = index.row();
    }

    foreach(QModelIndex index, deselected.indexes())
    {
        //取消数据库的索引
        QSqlRecord record = queryModel->record(index.row());
        db_index_deselected = record.value(0).toInt();
        //取消view索引
        deselected_rows = index.row();
    }

    if(selected_rows > -1)
    {
        list_selectedRows.push_back(selected_rows);
        //list_Select.push_back(db_index_selected);
        map_Select.insert(db_index_selected,0);
    }

    if(deselected_rows > -1)
    {
        list_selectedRows.removeOne(deselected_rows);
        qDebug()<<"取消" << db_index_deselected;
        map_Select.remove(db_index_deselected);
    }

    qSort(list_selectedRows.begin(),list_selectedRows.end());

    rowMap.insert(pageCurrent,list_selectedRows);

    QMapIterator<int, QList<int>> rowMapIterator(rowMap);
    rowMapIterator.toBack();
    while (rowMapIterator.hasPrevious())
    {
        rowMapIterator.previous();
        QList<int> temp = rowMapIterator.value();
        if(temp.isEmpty())
        {
            rowMap.remove(rowMapIterator.key());
        }

    }

}

将容器中的数据删除


DeletRows()
{    

    dbConn.transaction();

    QMapIterator<int, int> rowMapIterator(map_Select);
    rowMapIterator.toBack();
    while (rowMapIterator.hasPrevious())
    {
        rowMapIterator.previous();

        QString sql = QString("delete from %1 where %2 = %3").arg(tableName).arg(m_condition)
                .arg(rowMapIterator.key());
        dbConn.exec(sql);
    }

    dbConn.commit();    //提交事物
    //清空map
    tableView->clearSelection();
    rowMap.clear();
    map_Select.clear();

    if(rowMap.isEmpty())
    {
        qDebug()<<"删除完成";
    }

    if(m_flag == true)   //此处表示当前页面在搜索页面
    {
        CalculatorRecordsCount(m_str_sourfind_query);
        SourceManage_Find();
    }
    else {
        CalculatorRecordsCount(m_str_query);
        bindData(getPageSql());
    }

}

搜索条件下的翻页情况

在这里插入图片描述

全部删除搜索条件的资源
请添加图片描述

源码地址:
https://download.csdn.net/download/qq_42919993/84995311

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值