美文网首页
视图类--项目选择

视图类--项目选择

作者: 寒冰豌豆 | 来源:发表于2017-02-24 18:00 被阅读0次

视图中被选择的项目的信息存储在一个QItemSelectionModel中,这样被选择的索引被保持在一个独立的模型中,与所有的视图都是独立的。


当前项目和被选择的项目.png

标准的视图类中提供了默认的选择模型。
属于一个视图的选择模型可以使用selectionModel()来获得,
而且还可以在多个视图之间使用setSelectionModel()函数来共享选择模型。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QTableView;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    
private:
    Ui::MainWindow *ui;

    QTableView *tableView;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QStandardItemModel>
#include <QTableView>
#include <QDebug>


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

    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);
        }
    }
    tableView = new QTableView;
    tableView -> setModel(model);
    setCentralWidget(tableView);

   //试图中被选择的项目的信息存储在一个QItemSelectionModel中,这样被选择的索引被保持在一个独立的模型中,与所有的视图都是独立的。
    //获取视图的项目选择模型
    QItemSelectionModel *selectionModel =tableView -> selectionModel();

    //定义左上角和右下角的索引,然后使用俩个索引创建选择
    QModelIndex topLeft;
    QModelIndex bottomRight;
    topLeft = model ->index(1,1,QModelIndex());
    bottomRight = model ->index(5,2,QModelIndex());
    QItemSelection selection(topLeft,bottomRight);

    //使用指定的选择模式来选择项目
    selectionModel ->select(selection  , QItemSelectionModel::Select);
    //选中 指定所选择的项目的所有行的项目。
    //selectionModel ->select(selection, QItemSelectionModel::Select|QItemSelectionModel::Rows);

    /*
    这里先获取了视图的选择模型;
    要使用选择模型来选择视图中的项目,那么就必须指定 QItemSelection 和 选择模式 QItemSelectionModel::SelectionFlag。
    QItemSelection 是一个项目选择块,需要指定它的左上角和右下角的项目索引;
    而选择模式是选择模型更新时的一种方式,它是一个枚举变量,在QItemSelectionModel类中被定义,可以在帮助中察看它的值。
    这里QItemSelectionModel::Select 表示所指定的索引都将被选择;
    QItemSelectionModel::Toggle 表明指定索引的当前状态切换为相反的状态,如果项目以前没有被选择,那么现在被选择。
    QItemSelectionModel::SelectionFlag的值还可以使用位或运算符来使用,
*/
}

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

运行结果.png

下面来QItemSelectionModel::Toggle的效果:
在mianwindow.h中添加

public slots:
      void getCurrentItemData();
      void toggleSelection();
然后在cpp中的构造函数添加
ui->mainToolBar ->addAction(tr("当前项目"),this, SLOT(getCurrentItemData()));
ui->mainToolBar ->addAction(tr("切换选择"),this, SLOT(toggleSelection()));
void MainWindow::getCurrentItemData()
{
      qDebug()<<tr("获取当前的内容:")
                     <<tableView -> selectionModel()->currentIndex().data().toString();
}
void MainWindow::toggleSelection()
{
      QModelIndex topLeft = tableView ->model()->index(0,0,QModelIndex());
      QModelIndex bottomRight = tableView ->model()->index(tableView ->model()->rowCount(QModelIndex())-1,tableView ->model()->columnCount(QModelIndex())-1,QModelIndex());
      QItemSelection currentSelection(topLeft,bottomRight);
      tableView ->selectionModel() ->select(currentSelection,QItemSelectionModel::Toggle);
}
结果.png 结论.png

要获取选择模型中的索引,可以使用selectedIndex()函数,它会返回一个模型索引的列表,然后便利列表即可。
当选择模型中选择的项目改变时候,会发射相关信号。

相关文章

  • 视图类--项目选择

    视图中被选择的项目的信息存储在一个QItemSelectionModel中,这样被选择的索引被保持在一个独立的模型...

  • 视图类--项目选择2

    mainwindow.h mianwindow.cpp main.cpp

  • 2.7 django类视图

    类视图 在写视图的时候,Django除了使用函数作为视图,也可以使用类作为视图。使用类视图可以使用类的一些特性,比...

  • 最浅显易懂的Django系列教程(31)-类视图

    类视图 在写视图的时候,Django除了使用函数作为视图,也可以使用类作为视图。使用类视图可以使用类的一些特性,比...

  • Django类视图笔记整理

    Django基于类的视图 1.基于类的视图简介 基于类的视图使用Python 对象实现视图,它提供除函数视图之外的...

  • Django学习-第十二讲:视图高级(二)类视图、模板视图、列表

    1. 类视图 在写视图的时候,Django除了使用函数作为视图,也可以使用类作为视图。使用类视图可以使用类的一些特...

  • iOS 制作framework(功能类、资源+View+三方库)

    导出功能类 创建项目选择 Framework 创建功能类 配置项目 设置frameWork支持的架构image.p...

  • Flask系列教程(16)——类视图

    类视图 之前我们接触的视图都是函数,所以一般简称视图函数。其实视图也可以基于类来实现,类视图的好处是支持继承,但是...

  • 1.6 flask 类视图

    类视图 之前我们接触的视图都是函数,所以一般简称视图函数。其实视图也可以基于类来实现,类视图的好处是支持继承,但是...

  • 类视图

    之前我们接触的视图都是函数,所以一般简称视图函数。其实视图也可以基于类来实现,类视图的好处是支持继承,但是类视图不...

网友评论

      本文标题:视图类--项目选择

      本文链接:https://www.haomeiwen.com/subject/ykmbwttx.html