实例QT程序 —— QTableView 表格行的上下移动

目录

1.简介
2.源码
3.效果图



1.简介

上一篇文章,介绍了实现QTableWidget表格中行的上移/下移的功能,本次介绍QTableView表格中行的移动功能的实现(效果类似)。

2.源码

widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class QTableView;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_btnUp_clicked();

    void on_btnDown_clicked();

    void on_btnMove_clicked();

private:
    // 初始化表格
    void initForm();

    // 移动表格行
    void moveRow(QTableView *tableView, int currentRow, int toRow);

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H


widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QStandardItemModel>
#include <QDebug>
#include <QMessageBox>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    initForm();

    this->setWindowTitle("QTableView Move Row");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::initForm()
{
    QStringList labels({"列1", "列2", "列3"});
    QStandardItemModel *model = new QStandardItemModel(this);
    model->setHorizontalHeaderLabels(labels);

    for(int row=0; row<4; row++){
        for(int col=0; col<labels.size(); col++){
            QString strCell = QString("%1%2").arg(row+1).arg(col+1);
            QStandardItem *item = new QStandardItem(strCell);
            model->setItem(row, col, item);
        }
    }

    ui->tableView->setModel(model);

    int rowCount = model->rowCount();
    ui->sBoxFrom->setRange(1, rowCount);
    ui->sBoxTo->setRange(1, rowCount);
}

void Widget::moveRow(QTableView *tableView, int currentRow, int toRow)
{
    if( currentRow == toRow ){
        return;
    }

    QStandardItemModel *model = qobject_cast<QStandardItemModel*>(tableView->model());
    if (model == nullptr) {
        qDebug() << "model is null";
        return;
    }

    int column = tableView->currentIndex().column();
    if(nullptr == model || currentRow < 0 || currentRow > model->rowCount()-1)
    {
        QMessageBox::information(this,"tip","invalid");
        return ;
    }

    if( currentRow < toRow ){
        //下移需要判断最后一行,不移动
        if(currentRow == model->rowCount()-1)
        {
            QMessageBox::information(this,"提示","已经最后一行");
            return ;
        }
    }else{
        //上移需要判断是否第一行,不移动
        if(0 == currentRow)
        {
            QMessageBox::information(this,"提示","已经是第一行");
            return ;
        }
    }

    QList<QStandardItem *> listItem = model->takeRow(currentRow);
    model->insertRow(toRow,listItem);

    tableView->setCurrentIndex( model->index(toRow, column) );
    tableView->selectRow(toRow);
}


void Widget::on_btnUp_clicked()
{
    QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
    if (model == nullptr) {
        qDebug() << "model is null";
        return;
    }

    int row = ui->tableView->currentIndex().row();
    int nRowInsert = row - 1;
    moveRow(ui->tableView, row, nRowInsert);
}

void Widget::on_btnDown_clicked()
{
    QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
    if (model == nullptr) {
        qDebug() << "model is null";
        return;
    }

    int row = ui->tableView->currentIndex().row();
    int nRowInsert = row + 1;
    moveRow(ui->tableView, row, nRowInsert);
}

void Widget::on_btnMove_clicked()
{
    int nFrom = ui->sBoxFrom->value() - 1;
    int nTo = ui->sBoxTo->value() - 1;
    moveRow(ui->tableView, nFrom, nTo);
}


3.效果图

运行效果图
运行效果图




加油,向未来!GO~
Come on


发布了18 篇原创文章 · 获赞 3 · 访问量 3953

猜你喜欢

转载自blog.csdn.net/Redboy_Crazy/article/details/105153872
今日推荐