Qt之tableview显示更多数据

   数据库读取数据,一般在一页中显示256个数据,但为了在tabview中显示更多的数据,则需要做一下修改:

    QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("select id,time,co,h2s,o2,gas,ele from hdata");
    model->setHeaderData(0, Qt::Horizontal, tr("id"));
    model->setHeaderData(1, Qt::Horizontal, tr("Time"));
    model->setHeaderData(2, Qt::Horizontal, tr("Co"));
    model->setHeaderData(3, Qt::Horizontal, tr("H2s"));
    model->setHeaderData(4, Qt::Horizontal, tr("O2"));
    model->setHeaderData(5, Qt::Horizontal, tr("甲烷"));
    model->setHeaderData(6, Qt::Horizontal, tr("电量"));
 
  
    while(model->canFetchMore())
    {
       model->fetchMore();
    }
    ui->tableView->setModel(model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置成不可编辑

   需要加入fetch;

   关于fetch,在Qt帮助助手中,如下解释:

  bool QAbstractItemModel::canFetchMore ( const QModelIndex & parent ) const [virtual]
  Returns true if there is more data available for parent; otherwise returns false.
  The default implementation always returns false.
  If canFetchMore() returns true, QAbstractItemView will call fetchMore(). However, the fetchMore() function is only    called when the model is being populated incrementally.

  See also fetchMore().

意思就是说,如果想获得更多数据可以使用canFetchMore,如果canFetchMore返回true,则说明可以fetchMore().

你可能感兴趣的:(2.,QT,1.,C++)