当前位置: 代码迷 >> QT开发 >> 求tableWidget的用法解决方法
  详细解决方案

求tableWidget的用法解决方法

热度:76   发布时间:2016-04-25 03:53:39.0
求tableWidget的用法
通过一个字段查询数据库表,并将所有结果显示在tableWidget中,这怎么实现啊,求一段代码

------解决方案--------------------
查询一个字段:

QSqlQuery query;
query.prepare("select * from 表名 where 列名=:dname");
QString str = QString("%1").arg(所查询的变量名);
query.bindValue(":dname",str);
query.exec();

将所有结果显示在tableWidget:

while(query.next())
{
QLabel *label = new QLabel();
label->setText(query.value(0).toString());
ui->tableWidget->setCellWidget(1,0,label);
}

大致差不多这么写,凑合着用吧,自己再改改咯。
  相关解决方案