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

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

 

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

 

QStandardItemModel的简单应用

新建一个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);

最后效果:

QStandardItemModel的简单应用

原文链接:

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

相关文章:

  • 2022-01-05
  • 2021-10-27
  • 2021-12-22
  • 2021-06-11
  • 2021-05-24
  • 2021-11-20
  • 2021-08-25
  • 2021-06-17
猜你喜欢
  • 2021-09-20
  • 2022-12-23
  • 2021-12-18
  • 2021-07-11
  • 2021-08-09
  • 2021-12-22
  • 2021-11-27
相关资源
相似解决方案