欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

QTableView实现表格显示,自定义model

程序员文章站 2022-03-22 11:07:32
运行结果如下:代码如下:#include #include #include class CStudioTableModel :public QAbstractTableModel{public: CStudioTableModel(QObject *parent = 0); ~CStudioTableModel(); int rowCoun...

运行结果如下:

QTableView实现表格显示,自定义model

代码如下:

#include <QList>
#include <QStringList>
#include <QAbstractTableModel>

class CStudioTableModel :public QAbstractTableModel
{
public:
    CStudioTableModel(QObject *parent = 0);
    ~CStudioTableModel();
    int rowCount(const QModelIndex & parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role  = Qt::EditRole );

    void InsertItems(const QList<StudioInfo>& lstDatas);
    void InsertItem(const StudioInfo& dat);

    void DelItems();
    void DelItem(const QString& szStudioID);
protected:
    QStringList						m_header;
    QList<StudioInfo>               m_lstData;
};

CStudioTableModel::CStudioTableModel(QObject *parent /*= 0*/) :QAbstractTableModel(parent)
{
    m_header.clear();
     m_header << "ID" << "NAME" << "Address" << "Age"  << "Sex";
    m_lstData.clear();
}

CStudioTableModel::~CStudioTableModel()
{
}

int CStudioTableModel::rowCount(const QModelIndex & parent /*= QModelIndex()*/) const
{
    return m_lstData.count();
}

int CStudioTableModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
    return m_header.count();
}

QVariant CStudioTableModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
{
    if (section >= 0 && section < columnCount())
    {
        if (orientation == Qt::Horizontal)
        {
            switch (role)
            {
            case Qt::DisplayRole:
                return m_header[section];
                break;
            }
        }
        else
        {
            switch (role)
            {
            case Qt::DisplayRole:
                return section+1;
                break;
            }
        }
    }

    return QVariant();
}

QVariant CStudioTableModel::data(const QModelIndex & index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    int nRow = index.row();
    int nCol = index.column();
    if (nRow>= rowCount() || nRow<0 || nCol>= columnCount() || nCol<0)
    {
        return QVariant();
    }

    switch (role)
    {
    case Qt::DisplayRole:
        switch (nCol)
        {
        case 0:
            return m_lstData[nRow].szStudioID;
            break;
        case 1:
            return m_lstData[nRow].szStudioName;
            break;
        case 2:
            return m_lstData[nRow].szAddress;
            break;
        case 3:
            return m_lstData[nRow].nAge;
            break;
        case 4:
            return m_lstData[nRow].bMan ? "Male":"female";
            break;
        default:
            return QVariant();
            break;
        }
        break;
    }

     return QVariant();
}

bool CStudioTableModel::setData(const QModelIndex &index, const QVariant &value, int role /* = Qt::EditRole */)
{
    if (!index.isValid())
    {
        return false;
    }

    int nRow = index.row();
    int nCol = index.column();
    if (nRow>= rowCount() || nRow<0 || nCol>= columnCount() || nCol<0)
    {
        return false;
    }

    switch (role)
    {
    case Qt::EditRole:
        switch (nCol)
        {
        case 0:
            m_lstData[nRow].szStudioID = value.toString();
            break;
        case 1:
             m_lstData[nRow].szStudioName = value.toString();
            break;
        case 2:
            m_lstData[nRow].szAddress = value.toString();
            break;
        case 3:
            m_lstData[nRow].nAge = value.toInt();
            break;
        case 4:
            m_lstData[nRow].bMan = value.toBool();
            break;
        default:
           return false;
           break;
        }
        break;

    default:
        return false;
        break;
    }

    emit dataChanged(index, index);
    return true;
}

void CStudioTableModel::InsertItems(const QList<StudioInfo>& lstDatas)
{
    DelItems();
    int nCount = lstDatas.count();
    if (nCount > 0)
    {
        beginInsertRows(QModelIndex(), 0, nCount - 1);
        m_lstData = lstDatas;
        endInsertRows();
    }
}

void CStudioTableModel::InsertItem(const StudioInfo& dat)
{
    int nCount = rowCount();
    beginInsertRows(QModelIndex(), nCount, nCount);
    m_lstData.push_back(dat);
    endInsertRows();
}

void CStudioTableModel::DelItems()
{
    int nCount = rowCount();
    if (nCount > 0)
    {
        beginRemoveRows(QModelIndex(), 0, nCount - 1);
        m_lstData.clear();
        endRemoveRows();
    }
}

void CStudioTableModel::DelItem(const QString& szStudioID)
{
    for (int i=0; i<m_lstData.count(); i++)
    {
        if(m_lstData[i].szStudioID == szStudioID)
        {
            if (rowCount() > i)
            {
                beginRemoveRows(QModelIndex(), i, i);
                m_lstData.removeAt(i);
                endRemoveRows();
                break;
            }
        }
    }
}

QTableView是一个N行N列的控件,上述代码包括了修改某行的学生名字,增加一行/多行学生信息,删除一行/多行学生信息。需要注意的是增加要在beginInsertRows和endInsertRows之间做,而删除则是 beginRemoveRows()和endRemoveRows()之间。

rowCount用来通知model有多少行数据,columnCount用来通知model有多少列数据,data用来返回view的表格内容所需要的的数据,headerdata返回view的表格表头所需要的的数据,有垂直和水平的表头;Qt::Displayrole是model用来存储展现给用户的数据的,角色可以理解为数组,还有其他角色,读者可自行查看帮助文档。setData则是用来设置数据的,数据修改后要emit信号datachanged,这样view才会及时更新数据;

model的使用:

   QTableView* pTableView = new QTableView(this);
    CStudioTableModel* pTableModel = new CStudioTableModel(this);
    pTableView->setModel(pTableModel);
    pTableModel->InsertItems(lstData);

 

 

本文地址:https://blog.csdn.net/zsq1294110449/article/details/110198036

相关标签: Qt进阶开发