选择模型的使用

1、视图效果

2、源码:

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QTableView;
class QItemSelection;
class QModelIndex;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public slots:
    void getCurrentItemData();
    void toggleSelection();

    //选择模型中信号的使用和对选择的项目的处理
    //关联选择改变信号。新选择的项目、先前选择的项目
    void updateSelection(const QItemSelection &selected, const QItemSelection &deselected);
    //关联当前项改变信号。新的当前项的索引、先前的当前项的索引
    void changeCurrent(const QModelIndex &current, const QModelIndex &previous);
private:
    QTableView *table_view_;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include <QStandardItemModel>
#include <QTableView>
#include <QToolBar>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QStandardItemModel *model = new QStandardItemModel(7, 4, this);
    for(int row = 0; row <7; ++row){
        for(int column = 0; column < 4; ++column){
            QStandardItem *item = new QStandardItem(QString("%1").arg(row * 4 + column));
            model->setItem(row, column, item);
        }
    }
    table_view_ = new QTableView;
    table_view_->setModel(model);
    setCentralWidget(table_view_);

    //获取视图的项目选择模型
    QItemSelectionModel *selection_model = table_view_->selectionModel();
    //定义左上角和右下角的索引,然后使用这两个索引创建选择
    QModelIndex top_left;
    QModelIndex bottom_right;
    top_left = model->index(1, 1, QModelIndex());
    bottom_right = model->index(5, 2, QModelIndex());
    QItemSelection selection(top_left, bottom_right);
    //使用指定的选择模式来选择项目
    selection_model->select(selection, QItemSelectionModel::Select);

    QToolBar *tool = new QToolBar(this);
    addToolBar(tool);
    tool->addAction(tr("current"), this, &MainWindow::getCurrentItemData);
    tool->addAction(tr("toggle"), this, &MainWindow::toggleSelection);

    connect(selection_model, &QItemSelectionModel::selectionChanged, this, &MainWindow::updateSelection);
    connect(selection_model, &QItemSelectionModel::currentChanged, this, &MainWindow::changeCurrent);
}

MainWindow::~MainWindow()
{
}

void MainWindow::getCurrentItemData()
{
    qDebug() << "current content:" << table_view_->selectionModel()->currentIndex().data().toString();
}

void MainWindow::toggleSelection()
{
    QModelIndex top_left = table_view_->model()->index(0, 0, QModelIndex());
    QModelIndex bottom_right = table_view_->model()->index(table_view_->model()->rowCount(QModelIndex())-1,
                                                           table_view_->model()->columnCount(QModelIndex())-1, QModelIndex());
    QItemSelection cur_selection(top_left, bottom_right);
    table_view_->selectionModel()->select(cur_selection, QItemSelectionModel::Toggle);
}

void MainWindow::updateSelection(const QItemSelection &selected, const QItemSelection &deselected)
{
    QModelIndex index;
    QModelIndexList list = selected.indexes();
    //为选择的项目填充值
    foreach(index, list){
        QString text = QString("(%1, %2)").arg(index.row()).arg(index.column());
        table_view_->model()->setData(index, text);
    }
    list = deselected.indexes();
    //清空上一次选择的项目的内容
    foreach (index, list) {
        table_view_->model()->setData(index, "");
    }
}

void MainWindow::changeCurrent(const QModelIndex &current, const QModelIndex &previous)
{
    qDebug() << QString("move(%1, %2)to(%3, %4)").arg(previous.row()).arg(previous.column())
                .arg(current.row()).arg(current.column());
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值