QStandardItemModel的简单应用

The QStandardItemModel class provides a generic model for storing custom data.

QStandardItemModel提供了一个来存储数据的一般类型。

 

首先我们在ui上新建一个TabelView

 

新建一个QStandardItemModel的对象

this->myModel = new QStandardItemModel(this);

然后在TableView设置Model

ui->tableView->setModel(myModel);

最后为这个Model插入行及内容,并设置不同的颜色

    for(int i = 0;i < 5;i++)
    {
        myModel->insertRow(i,QModelIndex());
        this->myModel->setData(this->myModel->index(i,0),i);
        this->myModel->setData(this->myModel->index(i,1),"A");
    }
    this->myModel->item(0,0)->setBackground(Qt::green);
    this->myModel->item(1,0)->setBackground(Qt::red);
    this->myModel->item(2,0)->setBackground(Qt::yellow);
    this->myModel->item(3,0)->setBackground(Qt::blue);
    this->myModel->item(4,0)->setBackground(Qt::black);

最后效果:

原文链接:

代码下载:https://files.cnblogs.com/files/-Donny/QTableView.rar

posted @ 2017-01-16 00:52  东东Donny  阅读(1612)  评论(0编辑  收藏  举报