小编Mar*_*ark的帖子

当 main.qml 通过 HTTP 导入时,main.cpp 中的“engine.rootObjects().first()”崩溃

我们有一行代码,我一直(或更长时间)使用它来获取指向根窗口的指针。我们最近开始使用 HTTP 语法从服务器导入组件。当我们把它们放在一起时,程序崩溃,engine.rootObjects().first()返回空。这是一个简化的“main.cpp”:

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

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

    QObject* rootWindowPointer = (engine.rootObjects().first());        // <-- this is the function that fails

    return app.exec();
}
Run Code Online (Sandbox Code Playgroud)

这是一个简化的“main.qml”,其中包含导致问题的导入:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

import "http://eggs:88/empty"       // <-- this is the added line that caused the problem

ApplicationWindow …
Run Code Online (Sandbox Code Playgroud)

c++ qt qml

3
推荐指数
1
解决办法
964
查看次数

标签 统计

c++ ×1

qml ×1

qt ×1