首页 > 代码库 > 10.model/view实例(4)

10.model/view实例(4)

任务:给表单的每一列添加列名。

    技术分享

 

思考:

1.只需要添加一个函数 headerData()。 横向方面添加列名

 

代码如下:

QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role == Qt::DisplayRole) {
        if(orientation == Qt::Horizontal) {
            switch (section) {
            case 0:
                return QString("first");
                break;
            case 1:
                return QString("second");
                break;
            case 2:
                return QString("third");
                break;
            default:
                break;
            }
        }
    }
    return QVariant();
}

 

10.model/view实例(4)