zoukankan      html  css  js  c++  java
  • view/model

    main.cpp

    #include "Mainwindow.h"
    #include <QApplication>
    int main(int argc, char *argv[])
    {
       QApplication a(argc, argv);
        MainWindow w;
         w.show();
    
    
        return a.exec();
    }

    Mainwindow.h


    #include <QMainWindow>
    #include <QMenuBar>
    class QTableView;
    class QItemSelection;
    class QModelIndex;


    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~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 *tableView;
    QTableView *tableView2;
    QMenuBar *mainToolBar;
    };

    Mainwindow.cpp


    #include "mainwindow.h"

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

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {


    //******* 1. 选择模型的使用 ********//
    //设置基础数据
    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 *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);


    //******* 2. 获得选择项目、对选择项目进行反选 ********//

    //向主窗口工具栏中添加两个动作图标
    mainToolBar=new QMenuBar(this);
    mainToolBar->addAction(tr("当前项目"), this, SLOT(getCurrentItemData()));
    mainToolBar->addAction(tr("切换选择"), this, SLOT(toggleSelection()));


    //******* 3. 选择项目改变时动态设置 ********//

    //关联选择模型改变信号
    connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection))
    ,this, SLOT(updateSelection(QItemSelection,QItemSelection)));

    //关联选择项改变信号
    connect(selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex))
    ,this, SLOT(changeCurrent(QModelIndex,QModelIndex)));

    //***** 4. 当多个视图显示同一个模型的数据时,共享选择***//
    tableView2 = new QTableView;
    tableView2->setWindowTitle("tableView2");
    tableView2->resize(400, 300);
    tableView2->setModel(model);
    tableView2->setSelectionModel(selectionModel);
    tableView2->show();
    }

    MainWindow::~MainWindow()
    {

    delete tableView;
    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 curSelection(topLeft, bottomRight);
    tableView->selectionModel()->select(curSelection, 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());
    tableView->model()->setData(index, text);
    }

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

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

  • 相关阅读:
    Nginx得知——Hello World模
    Web静态和动态项目委托代理基于面向方面编程AOP
    PIC16SCM设置不同IO功耗端口状态的影响
    $.ajax通路RESTful Web Service一个错误:Unsupported Media Type
    什么是Entitlement
    加解密
    Types of Security Vulnerabilities
    fork后子进程从哪里开始执行
    进程间通信(IPC)介绍
    Using URL Schemes to Communicate with Apps
  • 原文地址:https://www.cnblogs.com/deadwood-2016/p/14196902.html
Copyright © 2011-2022 走看看