Skip to content
  • 0 Votes
    1 Posts
    720 Views
    No one has replied
  • 0 Votes
    6 Posts
    3k Views
    shaveraS

    @Chris-Kawa That's a valid point I hadn't considered. I'll just have to give this some more thought.

    It seems like maybe the most "Qt-correct" design would be to override childEvent(QChildEvent* event) to take the pointer into the member object when a child is added to the parent object. This would then, at least, bypass the need to call "findChild" over and over again.

  • 0 Votes
    1 Posts
    673 Views
    No one has replied
  • 0 Votes
    10 Posts
    3k Views
    N

    Thanks every body,
    I'm developing, a Bluetooth application. The bluetooth trames reception should manage the display, and I would like to be sure that nothing is forgotten. So when I receive a Bluetooth trame (after a request), I send an acknowledgement with checksum, and I send an other request when I finish the treatment of this trame. That's why I would like to be sure that the objects are created, to avoid error because of the modification of an object that does not exist.

  • 0 Votes
    3 Posts
    3k Views
    JKSHJ

    Qt developers have investigated this before, and there is a partially-working patch.

    There are still a number of important challenges to work through, but devs are focussing on other things at the moment. It is not currently being actively worked on: http://comments.gmane.org/gmane.comp.lib.qt.devel/10327

  • 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
    4 Posts
    2k Views
    A

    If your question was how to initialize QObject from child
    you do not need extra Parent variable,
    no extra deletes, just add it in constructor:

    Contact::Contact(int cat,QString fir,QString lst,QString ad,QString zp,
    QString ct,QString nm,QObject *parent)
    ::QObject( parent )
    {
    ..........
    }