其他分享
首页 > 其他分享> > QTableView合并单元格

QTableView合并单元格

作者:互联网

在这里插入图片描述
由于只能贴部分代码,所以下载请 qq:609162385,
或者下载地址:
//主要方式
QAbstractItemModel *m_HorizontalHeaderModel;
QAbstractItemModel *m_VerticalHeaderModel;

#ifndef CPROXYMODELWITHHEADERMODELS_H
#define CPROXYMODELWITHHEADERMODELS_H

#include <QIdentityProxyModel>
#include "ccustomheaderview.h"

class cProxyModelWithHeaderModels : public QIdentityProxyModel
{
    Q_OBJECT

public:
    explicit cProxyModelWithHeaderModels(QObject *parent = 0);

    void setHorizontalHeaderModel(QAbstractItemModel *model);
    void setVerticalHeaderModel(QAbstractItemModel *model);

protected:
    QVariant data(const QModelIndex &index, int role) const;

private:
    QAbstractItemModel *m_HorizontalHeaderModel;
    QAbstractItemModel *m_VerticalHeaderModel;
};

#endif // CPROXYMODELWITHHEADERMODELS_H

#include "cproxymodelwithheadermodels.h"

cProxyModelWithHeaderModels::cProxyModelWithHeaderModels(QObject *parent)
    : m_HorizontalHeaderModel(0)
    , m_VerticalHeaderModel(0)
    , QIdentityProxyModel(parent)
{

}

void cProxyModelWithHeaderModels::setHorizontalHeaderModel(QAbstractItemModel *model)
{
    int cnt;
    m_HorizontalHeaderModel = model;

    if(cnt = sourceModel()->columnCount())
    {
        emit(headerDataChanged(Qt::Horizontal, 0, cnt - 1));
    }
}

void cProxyModelWithHeaderModels::setVerticalHeaderModel(QAbstractItemModel *model)
{
    int rnt;
    m_VerticalHeaderModel = model;

    if ((rnt = sourceModel()->rowCount()))
    {
        emit(headerDataChanged(Qt::Vertical, 0, rnt - 1));
    }
}

QVariant cProxyModelWithHeaderModels::data(const QModelIndex &index, int role) const
{
    QVariant variant;

    if(role == cCustomHeaderView::HorizontalHeaderDataRole)
    {
        variant.setValue(static_cast<QObject *>(m_HorizontalHeaderModel));
        return variant;
    }
    else if(role == cCustomHeaderView::VerticalHeaderDataRole)
    {
        variant.setValue(static_cast<QObject *>(m_VerticalHeaderModel));
        return variant;
    }

    return QIdentityProxyModel::data(index, role);
}


标签:QAbstractItemModel,variant,单元格,合并,QTableView,VerticalHeaderModel,role,model,cPro
来源: https://blog.csdn.net/cqltbe131421/article/details/100554571