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

视图类--项目选择2

作者: 寒冰豌豆 | 来源:发表于2017-02-24 11:17 被阅读0次
model 和view的简单用法.png

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QTableView;
class QItemSelection;
class QModelIndex;

namespace Ui {
class MainWindow;
}

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

public slots:
    void getCurrentItemData();
    void toggleSelection();

    void updataSelection(const QItemSelection &selected, const QItemSelection &deleted);
    void changeCurrent(const QModelIndex &current, const QModelIndex &deleted);
private:
    Ui::MainWindow *ui;

    QTableView *tableView;
    QTableView *tableView2;
};

#endif // MAINWINDOW_H

mianwindow.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);
    ui->mainToolBar ->addAction(tr("当前项目"),this, SLOT(getCurrentItemData()));
    ui->mainToolBar ->addAction(tr("切换选择"),this, SLOT(toggleSelection()));

    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);
    //鼠标点击时,会选择一行;tableView2没有设置此函数,默认是选择一项
    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    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的值还可以使用位或运算符来使用,如QItemSelectionModel::Select|QItemSelectionModel::Rows
*/
    //selectionChanged()选择改变 信号--当前选择的项目法身改变时,会包含新选择的项目(参数1)和先前选择的项目(参数2)
    connect(selectionModel,SIGNAL(selectionChanged(QItemSelection,QItemSelection)),this,SLOT(updataSelection(QItemSelection,QItemSelection)));
    //currentChanged()当前项目改变时 发射信号--包含了新的当前项的索引(参数1)和先前项目的索引(参数2)
    connect(selectionModel,SIGNAL(currentChanged(QModelIndex,QModelIndex)),this,SLOT(changeCurrent(QModelIndex,QModelIndex)));

    //当多个视图显示同一个model时,只要使用setSelectionModel()为他们设置相同的选择模型,那么就可以实现共享选择。
    tableView2 = new QTableView;
    tableView2 ->setWindowTitle("tableView2");
    tableView2 ->resize(400,300);
    tableView2->setModel(model);
    tableView2 ->setSelectionModel(selectionModel);
    tableView2 ->show();
}

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

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);
}
void MainWindow::updataSelection(const QItemSelection &selected, const QItemSelection &deseleted)
{
    QModelIndex index;

    //这里使用indexes()来获取所有选择项目的索引,然后重新为他们赋值。。。。。。。
    QModelIndexList list = selected.indexes();
    //为现在选择的项目填充值,
    foreach(index,list){
        QString text = "("+QString::number(index.row())+","+QString::number(index.column())+")";
        qDebug()<<"r c "<<index.row()<<index.column();
        tableView ->model()->setData(index,text);
    }
    list = deseleted.indexes();

    //清空上一次选择的项目内容
    foreach(index,list){
        tableView -> model()->setData(index,"");
    }
}

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

main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

#include <QTextCodec>

int main(int argc, char *argv[])
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}

相关文章

网友评论

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

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