3

I want get json file from local and send it to QML using this:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    engine.rootContext()->setContextProperty("text_json", "{\"text1\": \"Loading\"}");
    QObject::connect(
                &engine,
                &QQmlApplicationEngine::objectCreated,
                &app,
                [url](QObject *obj, const QUrl &objUrl) {
                    if (!obj && url == objUrl)
                        QCoreApplication::exit(-1);
                },
                Qt::QueuedConnection
    );
    engine.load(url);

    return app.exec();
}

But it say that QQmlEngine::setContextForObject(): Object already has a QQmlContext but I don't understand anything from that default file.

I don't have found anything from now.

-- Added Main.qml --

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    id: window
    width: 640
    height: 480
    visible: true
    title: qsTr("Stack")

    property var text_json: ({"text1": "Loading"})

    header: ToolBar {
        contentHeight: toolButton.implicitHeight

        ToolButton {
            id: toolButton
            icon.source: "./images/ruby.png"

            font.pixelSize: Qt.application.font.pixelSize * 1.6
            onClicked: {
                drawer.open()
            }
        }

        Label {
            text: stackView.currentItem.title
            anchors.centerIn: parent
        }
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Tryliom
  • 895
  • 1
  • 12
  • 37
  • I don't see any problem with the code that you've shown. It works for me when I use that same `setContextProperty` call. – JarMan Feb 15 '21 at 15:48
  • Are you explicitly calling `setContextForObject` somewhere? – JarMan Feb 15 '21 at 15:50
  • I will post my main.qml to see if I have put this somewhere but actually no – Tryliom Feb 15 '21 at 15:55
  • @Tutturuuu what is your Qt version? – eyllanesc Feb 15 '21 at 16:00
  • @eyllanesc v6.1.0 – Tryliom Feb 15 '21 at 16:19
  • 6.1.0 is not released yet, is it? I notice you have a `text_json` property in your qml, but you are also adding it as a context property in main.cpp. You should get rid of one of those. But that's not causing your problem. – JarMan Feb 15 '21 at 16:24
  • @JarMan I have these version or I don't know where to find the version of Qt https://prnt.sc/zmfsiw; I have delete the text_json in qml but it doesn't work either – Tryliom Feb 15 '21 at 16:30
  • I have verify that is not an error that come from another file but not at all, still the same error – Tryliom Feb 16 '21 at 07:41
  • This bug seems to be related to the message: https://bugreports.qt.io/browse/QTBUG-89827 – Hyndrix Mar 11 '21 at 18:07

1 Answers1

7

Maybe a bit late, but did you try importing in your Main.qml without version number?

import QtQuick
import QtQuick.Controls
Florian Bailly
  • 106
  • 1
  • 6