gpt4 book ai didi

json - 填充 QTableView 表单 JSON 数组

转载 作者:行者123 更新时间:2023-12-03 18:21:37 29 4
gpt4 key购买 nike

我正在开发一个 Web 服务客户端,我可以在其中获取一组 JSON 对象,并且我想用它们填充 QTableView。 JSON 键应该是列标题,值应该是每一行数据。

JSON 数组示例:

[{"id":"1","name":"Robotina","type":"droid","year":"1970"},{"id":"2","name":"Astro Boy","type":"droid","year":"2015"},{"id":"3","name":"Terminator","type":"droid","year":"2020"},{"id":"4","name":"Bender","type":"droid","year":"3000"},{"id":"5","name":"C-3PO","type":"droid","year":"1978"},{"id":"6","name":"R2D2","type":"droid","year":"1977"},{"id":"7","name":"Mazinger Z","type":"droid","year":"1987"},{"id":"8","name":"Robocop","type":"droid","year":"1987"},{"id":"9","name":"WALL-E","type":"mechanical","year":"2200"},{"id":"10","name":"EVE","type":"mechanical","year":"2200"}]

最佳答案

我有一个类似的问题,因为我的 Json 查询作为散列数组返回。

我在这里整理了一个 QJsonTableModel:https://github.com/poulh/qjsontablemodel

QJsonTableModel::Header header;
header.push_back( QJsonTableModel::Heading( { {"title","Title"}, {"index","title"} }) );
header.push_back( QJsonTableModel::Heading( { {"title","Season"}, {"index","season"} }) );
header.push_back( QJsonTableModel::Heading( { {"title","Episode"}, {"index","episode"} }) );
header.push_back( QJsonTableModel::Heading( { {"title","Air Date"}, {"index","air_date"} }) );

episodes = new QJsonTableModel( header, this );
ui->episodeTableView->setModel( episodes );

QString json = "[{\"series\":\"Black Sails\",\"season\":1,\"episode\":1,\"title\":\"I.\",\"air_date\":\"2014-01-25\"},{\"series\":\"Black Sails\",\"season\":1,\"episode\":2,\"title\":\"II.\",\"air_date\":\"2014-02-01\"}]";
QJsonDocument jsonDocument = QJsonDocument::fromJson( json );
episodes->setJson( jsonDocument );

然后,您可以通过 QModelIndex 检索该行的 Json 对象。无论您单击哪个单元格,它都会为您提供整行的 Json 对象。
void TVTime::on_episodesTableView_doubleClicked(const QModelIndex &index)
{
QJsonObject object = episodes->getJsonObject( index );
qDebug() << object["title"];
}

让我知道它是否有效。

关于json - 填充 QTableView 表单 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26148188/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com