我要为这个下层的每个结果计算新行
QSqlQueryModel *model = new QSqlQueryModel();
model->setQuery("SELECT height,age from patients", DB->getDB());
model->setHeaderData(4,Qt::Horizontal,tr("height"));
model->setHeaderData(2,Qt::Horizontal,tr("age"));
ui->tableView->setModel(model);
ui->tableView->show();
作为
ratio = height/age;
我可以吗?怎么办?
谢谢!
最佳答案
你可以的。
您可以访问 QSqlQueryModel
中的项目,例如:
QSqlRecord rec = model->record(row); // get the row you need
QVariant height = rec.field("height").value(); // or access it via index
QVariant age = rec.field("age").value();
double ratio = height.toDouble() / age.toDouble();
只需根据您的需要调整代码,并可能为其添加值检查。
干杯
关于c++ - 将计算列添加到 Qt QSqlQueryModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29644959/