QTableView,Model/View的运用(1),QAbstractTableModel

来源:互联网 发布:阿里云机顶盒刷机固件 编辑:程序博客网 时间:2024/04/30 09:18

Qt的Model/View模式相当强大,下面我们就来看一个QTableView运用QAbstractTableModel的例子。

由于QAbstractTableModel是个抽象类,所以必须自己重新写一个类来继承它。

下面就是我自己写的一个类TableView

头文件:TableModel.h

#ifndef TABLEMODEL_H#define TABLEMODEL_H#include <QAbstractTableModel>class TableModel : public QAbstractTableModel{    Q_OBJECTpublic:    explicit TableModel(QObject *parent = 0);    /**     * @brief rowCount 虚函数,重写     * @param parent     * @return  返回table的行     */    virtual int rowCount(const QModelIndex &parent) const;    /**     * @brief columnCount 虚函数,重写     * @param parent     * @return  返回table的列     */    virtual int columnCount(const QModelIndex &parent) const;    /**     * @brief data  虚函数,重写     * @param index     * @param role  控制每个Index的角色     * @return 返回每个Index的值     */    virtual QVariant data(const QModelIndex &index, int role) const;    /**     * @brief headerData    虚函数,重写     * @param section       header的索引,依次递增     * @param orientation   横向头/纵向头     * @param role          每个headIndex的角色     * @return              返回每个headIndex的值     */    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;};#endif // TABLEMODEL_H
源文件TableModel.cpp:

#include "tablemodel.h"TableModel::TableModel(QObject *parent) :    QAbstractTableModel(parent){}int TableModel::rowCount(const QModelIndex &parent) const{    return 5;//5行}int TableModel::columnCount(const QModelIndex &parent) const{    return 4;//5列}QVariant TableModel::data(const QModelIndex &index, int role) const{    if(!index.isValid())        //安全判断    {        return QVariant();    }    if(Qt::TextAlignmentRole == role)    {        return int(Qt::AlignHCenter | Qt::AlignVCenter);    }    else if(Qt::DisplayRole == role)            //这里这样写是为了过滤掉其他role(例如过滤掉checkBox)    {        return 1;//在所有的index中显示1(当然也可以根据index的不同显示不同的值)    }    return QVariant();}QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const{    if(Qt::DisplayRole != role)    {        return QVariant();    }    if(orientation == Qt::Horizontal)       //横向头    {        return section + 1;//横向头一次递增    }    else if(orientation == Qt::Vertical)    //纵向头    {        return section + 1;//纵向头依次递增    }    return QVariant();}

这样,一个自己的tableModel类就已经完成了。

下面建立一个UI界面,MainWindow来做测试

MainWindow.h

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>class TableModel;class QSqlTableModel;namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();private:    void initUI();private:    Ui::MainWindow *ui;    TableModel*           m_pModel;};#endif // MAINWINDOW_H

MainWindow.cpp

#include "MainWindow.h"#include "ui_mainwindow.h"#include "tablemodel.h"MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);    initUI();}MainWindow::~MainWindow(){    delete ui;}void MainWindow::initUI(){    m_pModel = new TableModel(ui->tableView);    ui->tableView->setModel(m_pModel);}

实现效果图如下:


0 0
原创粉丝点击