Qt之tableview显示更多数据

37 篇文章 1 订阅

   数据库读取数据,一般在一页中显示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().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值