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 ¤t, 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 ¤t, const QModelIndex &previous)
{
qDebug() << QString("move(%1, %2)to(%3, %4)").arg(previous.row()).arg(previous.column())
.arg(current.row()).arg(current.column());
}