Skip to content
  • Parsing JSON with QML

    Unsolved QML and Qt Quick json
    3
    0 Votes
    3 Posts
    5k Views
    SikarjanS
    Hi, I am using the one dridk mentioned. The parsing works fine and I use listModel.append({jsonObject}); to fill my ListView. Everything is in the ListModel besides z. If I do: for(var i in jsonObject){ eventModel.append(jsonObject[i]) eventModel.set(i, {"zn":jsonObject[i]["z"][0]}) } z is added as an array. Just like in the JSON object just without the []-brackets.
  • How to I prepare models for Canvas3D view?

    Unsolved QML and Qt Quick 3d canvas mesh json
    2
    0 Votes
    2 Posts
    1k Views
    U
    Try to triangulate faces of your mesh (select your shape, switch to Edit Mode and press Ctrl+T ).
  • 0 Votes
    2 Posts
    6k Views
    nazimGTN
    resolved !! i've chenged m_pNetworkAccessManager->get(request); by request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); QByteArray par=""; m_pNetworkAccessManager->post(request,par);
  • Process returned JSON in webview

    General and Desktop json webview qt quick qml
    4
    0 Votes
    4 Posts
    2k Views
    QjayQ
    In answer to my original question i did something like this onUrlChanged: { console.log(searchwebview.url); test_json(); } and i made this function to parse the returned JSON ( this function is not perfect yet ) function test_json() { var p_url = searchwebview.url var http = new XMLHttpRequest(); var json , parse , text , rev_id; http.onreadystatechange = function(){ if(http.readyState == 4 && http.status == 200) { json = http.responseText; parse = JSON.parse(json); rev_id = parse.parse.revid; console.log(rev_id); text = parse.parse.text["*"]; //console.log(text); // <-- STRIP ME (o.O) while(text.match(/&#39;\/index.php/)){ text = text.replace(/&#39;\/index.php/, "http://en.wikitolearn.org/index.php"); text = text.replace(/&amp;/,"&"); text = text.replace(/MathShowImage&amp;/, "MathShowImage&") text = text.replace(/mode=mathml&#39;/, "mode=mathml\""); text = text.replace(/<meta class="mwe-math-fallback-image-inline" aria-hidden="true" style="background-image: url\(/ ,"<img style=\"background-repeat: no-repeat; background-size: 100% 100%; vertical-align: -0.838ex;height: 2.843ex; \" src=\""); text = text.replace(/<meta class="mwe-math-fallback-image-display" aria-hidden="true" style="background-image: url\(/ ,"<img style=\"background-repeat: no-repeat; background-size: 100% 100%; vertical-align: -0.838ex;height: 2.843ex; \" src=\""); text = text.replace(/&amp;mode=mathml\"/ , "&mode=mathml>\""); text = styling + text; } console.log(text); // after strip :p . webview.loadHtml(text); } }; http.open('GET',p_url); http.send(); }
  • How to access this JSON ( indented JSON )

    Solved General and Desktop json
    9
    0 Votes
    9 Posts
    3k Views
    QjayQ
    Crap i mixed 2 codes. Sorry for that :m. Thank you everyone
  • 0 Votes
    12 Posts
    6k Views
    A
    @Gojir4 Sure, Thanks for your Help.
  • Creating JSON files from scratch

    Solved General and Desktop json create
    11
    0 Votes
    11 Posts
    7k Views
    M
    @Paul-Colby YES!! I can't believe I didn't see it. I guess I didn't read it carefully. Thank you!!!!
  • Create Json using QJsonDocument

    Solved General and Desktop json qjsondocument
    3
    0 Votes
    3 Posts
    34k Views
    4
    @Paul-Colby Thank you
  • 0 Votes
    1 Posts
    923 Views
    No one has replied
  • [SOLVED]parse Json in Qt5

    General and Desktop json json parser
    7
    0 Votes
    7 Posts
    29k Views
    4
    @yeckel Thank you very much it's working now json file [{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}] Qt code QFile file; file.setFileName("test1.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); QJsonParseError jsonError; QJsonDocument flowerJson = QJsonDocument::fromJson(file.readAll(),&jsonError); if (jsonError.error != QJsonParseError::NoError){ qDebug() << jsonError.errorString(); } QList<QVariant> list = flowerJson.toVariant().toList(); QMap<QString, QVariant> map = list[0].toMap(); qDebug() << map["name"].toString();
  • 0 Votes
    1 Posts
    1k Views
    No one has replied
  • 0 Votes
    2 Posts
    3k Views
    benlauB
    Not quite understand your question.. Generally speaking, Javascript application use a Promise object for deferred and asynchronous operation. For example , do something when two or more asynchronous operations completed (in any order). Qt do not bundle any Promise implementation by default. You could get a one from my github repo: benlau/quickpromise Example Promise { resolveWhen: Q.all([endpoint1. onReplyStatusChanged,endpoint2. onReplyStatusChanged]); onFulfilled: { // It will be triggered only if both of the endpoint emitted the signal controller.setData1(); controller.setData2(); } }
  • 0 Votes
    7 Posts
    9k Views
    GGAllinG
    @SGaist Hi SGaist, i solved manupulating my BA manually int bW = bitmap.value("width").toInt(); int bH = bitmap.value("height").toInt(); QString shortFileName = bitmap.value("id").toString(); QString fileName = btb::ResourceManager().getClipDir().absolutePath().append("/"+shortFileName+".png"); QByteArray ba = bitmap.value("rect").toByteArray(); QByteArray to = QByteArray(); for(int i=0; i<bWbH;i++) { // ARGB -> ABGR to.append( ba[i4+3]); to.append( ba[i4+2]); to.append( ba[i4+1]); to.append( ba[i4]); } QImage image((const unsigned char)to.data(),bW,bH,QImage::Format_RGB32); //image.invertPixels(); image.save(fileName,"PNG"); thanks for help!
  • 0 Votes
    6 Posts
    3k Views
    A
    Thanks for the tip, I'll try it :).
  • 0 Votes
    3 Posts
    2k Views
    L
    I finally found a method to solve the problem. The following is the code snippet. main.cpp #include <QGuiApplication> #include <QStringList> #include <qqmlengine.h> #include <qqmlcontext.h> #include <qqml.h> #include <QtQuick/qquickitem.h> #include <QtQuick/qquickview.h> #include <jsondata.h> int main(int argc, char ** argv) { QGuiApplication app(argc, argv); QStringList datalist; Jsondata jsondata; jsondata.datalistmethod(); datalist = jsondata.datalist; QQuickView view; QQmlContext *ctxt = view.rootContext(); ctxt->setContextProperty("myModel", QVariant::fromValue(datalist)); view.setSource(QUrl("qrc:main.qml")); view.show(); return app.exec(); } jsondata.h #ifndef JSONDATA_H #define JSONDATA_H #include <QObject> #include <QNetworkReply> #include <QStringList> #include <QNetworkAccessManager> class Jsondata : public QObject { Q_OBJECT public: QStringList datalist; explicit Jsondata(QObject *parent =0); void datalistmethod(); public slots: void onResult (QNetworkReply*); private: QNetworkAccessManager *manager; }; jsondata.cpp #include "jsondata.h" #include <QNetworkReply> #include <QNetworkRequest> #include <QNetworkAccessManager> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QEventLoop> #include <QtQuick/qquickitem.h> #include <QtQuick/qquickview.h> #include <QTimer> #include <qqmlengine.h> #include <qqmlcontext.h> #include <qqml.h> Jsondata::Jsondata(QObject *parent) : QObject(parent) { } void Jsondata::datalistmethod() { // Now parse this JSON according to your needs ! manager = new QNetworkAccessManager(this); manager->setNetworkAccessible(QNetworkAccessManager::Accessible); QNetworkRequest request; QEventLoop eventloop; QUrl url("http://***/api/web/v1/links"); request.setUrl(url); QNetworkReply *reply = manager->get(request); connect(reply, SIGNAL(finished()), &eventloop, SLOT(quit())); eventloop.exec(); onResult(reply); } void Jsondata::onResult(QNetworkReply* reply) { QString data = (QString) reply->readAll(); qDebug() << "Response:" << data; QJsonDocument jsonResponse = QJsonDocument::fromJson(data.toUtf8()); QJsonArray jsonArray = jsonResponse.array(); foreach (const QJsonValue & value, jsonArray) { QJsonObject obj = value.toObject(); datalist.append(obj["name"].toString()); datalist.append(obj["link"].toString()); } } By adding event loop, the datalist method will wait for the onResult method completed to execute next line of code. This is using the example of string list model. If anyone feel it useful for reference, please +1 my post. Thank you. ^^
  • 0 Votes
    1 Posts
    807 Views
    No one has replied
  • 0 Votes
    2 Posts
    5k Views
    ?
    Hi @LuisAbreu! I cannot confirm this. Are you sure your json file is valid? The following works for me: ============================================ Json file: { "className": "ShoppingCart", "closedOn": "Fri Mar 27 15:52:13 2015", "coupon_count": "0", "createdOn": "Fri Mar 27 15:51:09 2015", "list": [ { "_className": "Product", "discount": "0", "fromyoubeep": "1", "has_alarm": 0, "id": "5601151333457", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "loyaltyCredit": -1, "min_age": 0, "name": "Sumo Compal Cl<C3><A1>ssico Tutti-Fruti 1lt", "userCreated": "0", "weight": "0" }, { "_className": "Product", "fromyoubeep": "0", "has_alarm": 0, "id": "", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "userCreated": "0", "weight": "0" } ], "loyaltyCard": { "_className": "LoyaltyCard", "barcode": "2446037038353", "barcodeType": "EAN13", "discount": "0", "fromyoubeep": "1", "has_alarm": 0, "id": "2446037038353", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "loyaltyCredit": 0, "min_age": 0, "name": "Cart<C3><A3>o Poupa Mais", "price": "0", "product_id": "-1", "quantity": "0", "quantityValidated": "0", "state": "0", "type": 1, "userCreated": "0", "weight": "0" }, "mobile_checkout": "1" } ============================================ C++ code: QFile file("/home/pw/file.json"); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "file error"; return; } const QByteArray ba = file.readAll(); file.close(); QJsonParseError err; QJsonDocument doc = QJsonDocument::fromJson(ba, &err); qDebug() << err.errorString(); qDebug() << err.offset; QJsonObject sett2 = doc.object(); qDebug() << sett2.isEmpty(); QJsonObject sett3 = sett2.value(QString("loyaltyCard")).toObject(); qDebug() << sett3.isEmpty(); QJsonValue sett4 = sett3.value(QString("barcode")); qDebug() << sett4.toString(); ============================================ Cheers!
  • 0 Votes
    9 Posts
    13k Views
    JKSHJ
    @bundickt said: @JKSH The output of qDebug() << data; is: QVariant(QJSValue, ) The output of data.value<QJSValue>().toVariant() is: QVariant(QVariantMap, QMap((key1, QVariant(QString, value1) ) ( key2 , QVariant(QString, value2) ) ) ) Ah, right. I thought the QML engine would automatically convert the QJSValue to a QVariantMap, but you had to do the conversion yourself. Thank you for sharing your findings!
  • QJson Parsing help request

    General and Desktop json
    3
    0 Votes
    3 Posts
    2k Views
    JKSHJ
    Hi, @metaDom said: QJsonDocument jsonResponse = QJsonDocument::fromJson(ba); QJsonObject jsonObj = jsonResponse.object(); id = jsonObj["id"].toString(); This works fine to get upper level values, but not the sub ones. Any ideas? Thanks in advance! You get the sub ones in the same way. "cat" is an object, so you convert the value to an object: QJsonObject catObj = jsonObj["cat"].toObject(); "cat" is an object that contains "test", which is an array that contains 1 object that contains your 5 string elements. This page might be useful: http://doc.qt.io/qt-5/qjsonvalue.html
  • Parse multiple Json

    General and Desktop json
    4
    0 Votes
    4 Posts
    2k Views
    M
    Hi, usually in TCP is a best practice to do one of the following: Send a header with some sync info (special values, length, ...) Using New-line for text information Are you sure there's no special character between JSON objects? Keep in mind that QJsonDocument doc(QJsonDocument::fromJson(json_data)); json_data.remove(0, doc.toJson().length()); could not work because QJsonDocument, make some formatting on the string so doc.toJSon().length() could return a value that is not the same you read.