一个 只读 readonly 示例

stringListModel.cpp

#ifndef STRINGLISTMODEL_H
#define STRINGLISTMODEL_H

#include <QAbstractListModel>
#include <QStringList>
class StringListModel : public QAbstractListModel
{
public:
    StringListModel(QObject *parent):QAbstractListModel(parent){}
//对QAbstractModel 的部分纯虚函数进行实现
    int columnCount(const QModelIndex &parent) const;
    int rowCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;

private:
    QStringList StringList;
};

#endif // STRINGLISTMODEL_H

stringlistmodel.cpp

#include "stringlistmodel.h"
#include <qDebug>
#include <QFont>
#include <QBrush>
#include <QPixmap>

int StringListModel::columnCount(const QModelIndex &) const
{
    return 3;
}
int StringListModel::rowCount(const QModelIndex &) const
{
    return 2;
}

QVariant StringListModel::data(const QModelIndex &index, int role) const
{
    int row = index.row();
    int column = index.column();
    qDebug()<<QString("Row%1,Column%2").arg(index.row()+1).arg(index.column()+1);

    switch(role){

    case Qt::BackgroundRole ://背景
        if(row == 0 && column == 2){
            QBrush color(Qt::red);

            return color;
        }
        if(row == 1 && column == 2){
            QPixmap pix("../images/1.jpg");
            return pix;
        }
        break;
    case Qt::DisplayRole : //展示
        if(row == 0 && column == 1) return QString("<---LEFT");
        if(row == 1 && column == 1) return QString("RIGHT--->");
        return QString("Row%1,Column%2").arg(index.row()+1).arg(index.column()+1);
        break;
    case Qt::FontRole : //字体
        if(row == 0 && column == 0){
            QFont boldFont;
            boldFont.setBold(true);
            return boldFont;
        }
        break;
    case Qt::TextAlignmentRole :  //对齐
        if(row == 1 && column == 1){
            return Qt::AlignRight + Qt::AlignVCenter;
        }
        break;
    case Qt::CheckStateRole :  //check框
        if(row == 1 && column == 0){
            return Qt::Checked;
        }
    }
    return QVariant();
}

QVariant StringListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    return QVariant();
}

main.h

#include "widget.h"
#include "stringlistmodel.h"
#include <QApplication>
#include <QTableView>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTableView * view = new QTableView;
    StringListModel * myModel = new StringListModel(0);
    view->setModel(myModel);
    view->resize(500,300);
    view->show();

    return a.exec();
}

The role parameter is used to let the model know which property is being requested.
ItemDataRole是一个枚举变量,可以在文档里查到:

ItemDataRole-enum

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值