QTableView 实现表格及相关用法(C++)(QStandardItemModel+QItemSelectionModel)

关于QTableView的基础功能使用有很多相关博客,此处不再复述,具体可以参考:

1、Qt QStandardItemModel(1.超级详细用法)_qtablemodel用法-CSDN博客

2、【Qt笔记】QTableView控件详解-CSDN博客

本文主要做以下两点功能的补充:

1、对QTableView表格选中行 内容做上下移动

2、对QTableView表格选中行删除

一、初始化表格

//假设已有tabview组件,名称为tabView

   QStandardItemModel *model;            //创建一个模型
   QItemSelectionModel *theSelection;     //创建模型索引

   model = new QStandardItemModel();
   theSelection = new QItemSelectionModel(model);

   ui->tabView->setModel(model);        //设置模型
   ui->tabView->setSelectionModel(theSelection)    //设置模型索引

   ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
   ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);

   //设置表格列字段名
    model->setColumnCount(3);//设置3列
    model->setHeaderData(0,Qt::Horizontal,"学号");
    model->setHeaderData(1,Qt::Horizontal,"名称");
    model->setHeaderData(2,Qt::Horizontal,"电话");
    //设置内容
    model->setItem(0,0,new QStandardItem("001"));
    model->setItem(1,0,new QStandardItem("002"));
    model->setItem(2,0,new QStandardItem("003"));

    model->setItem(0,1,new QStandardItem("李华"));
    model->setItem(1,1,new QStandardItem("小明"));
    model->setItem(2,1,new QStandardItem("老王"));

    model->setItem(0,2,new QStandardItem("111"));
    model->setItem(1,2,new QStandardItem("222"));
    model->setItem(2,2,new QStandardItem("333"));

二、选中某行内容,点击按钮触发"删除"

1、按钮绑定函数

//假设已有按钮,名称为pushButton_remove

//头文件中:
private slots:
    void RemoveRow();

//cpp中,初始化时:
//pushButton_remove按钮绑定RemoveRow()函数
connect(ui->pushButton_remove,SIGANL(clicked(bool)),this,SLOT(RemoveRow()));

2、写一个删除选中行的函数

void YourQtProject::RemoveRow()
{
    QModelIndex curIndex = theSelection->currentIndex();
    //如果选中行是最后一行,则无需重新排序
    if(curIndex.row()==model->rowCount()-1)
    {
        model->removeRow(model->rowCount()-1);
    }
    else
    {
        //删除当前选中行,并且重新排序序号
        model->removeRow(curIndex.row());
        theSelection->setCurrentIndex(curIndex,QItemSelectionModel::Select);
    }
}

三、选中某行内容,点击按钮触发"向上移动"&"向下移动"

1、按钮绑定函数

//假设已有向上移动按钮,名称为pushButton_Upmove
//假设已有向上移动按钮,名称为pushButton_Downmove

//头文件中:
private slots:
    void Upmove();
    void Downmove();

//cpp中,初始化时:
//pushButton_remove按钮绑定RemoveRow()函数
connect(ui->pushButton_Upmove,SIGANL(clicked(bool)),this,SLOT(Upmove()));
connect(ui->pushButton_Downmove,SIGANL(clicked(bool)),this,SLOT(Downmove()));

2、写"向上移动"&"向下移动"的函数

void YourQtProject::Upmove()
{
    //获取选中的行序号
    QModelIndex curIndex = theSelection->currentIndex();
    //如果选中行不是第一行,就可以向上移动
    if(curIndex.row()!=0)
    {
        for(int i = 0;i<model->columnCount();++i)
        {
            QStandarItem* item1 = model->takeItem(curIndex.row(),i);
            QStandarItem* item2 = model->takeItem(curIndex.row()-1,i);
            //本质是选中行与上一行互换内容
            model->setItem(curIndex.row(),i,item2);
            model->setItem(curIndex.row()-1,i,item1);
        }
    }
}

void YourQtProject::Downmove()
{
    //获取选中的行序号
    QModelIndex curIndex = theSelection->currentIndex();
    //如果选中行不是最后一行,就可以向下移动
    if(curIndex.row()!=model->rowCount()-1)
    {
        for(int i = 0;i<model->columnCount();++i)
        {
            QStandarItem* item1 = model->takeItem(curIndex.row(),i);
            QStandarItem* item2 = model->takeItem(curIndex.row()+1,i);
            //本质是选中行与下一行互换内容
            model->setItem(curIndex.row(),i,item2);
            model->setItem(curIndex.row()+1,i,item1);
        }
    }
}

本文内容为笔者在网页手写编辑,可能存在疏忽错误导致内容或格式不对,如遇,麻烦指出,感谢!

相关推荐
攻城狮7号1 小时前
【第四节】C++设计模式(创建型模式)-Builder(建造者)模式
c++·设计模式·建造者模式
fpcc1 小时前
设计心得——解耦的实现技术
c++·软件工程
水瓶丫头站住1 小时前
Qt中QRadioButton的样式设置
开发语言·qt
东方芷兰1 小时前
算法笔记 04 —— 算法初步(下)
c++·笔记·算法
xinghuitunan2 小时前
时间转换(acwing)c/c++/java/python
java·c语言·c++·python
@hdd2 小时前
深入理解 QObject的作用
qt
TechNomad2 小时前
C++访问MySQL数据库
数据库·c++·mysql
Emplace2 小时前
ABC381E题解
c++·算法