實例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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章