Qt:QtQuick:使用Qt静态库无法找到qml

简介

调试运行提示

QQmlApplicationEngine failed to load component
qrc:/main.qml:2 module "QtQuick.Controls" is not installed
qrc:/main.qml:1 module "QtQuick" is not installed
qrc:/main.qml:2 module "QtQuick.Controls" is not installed

解决方案

看了几篇文章解决方案都不优雅,提供一个自认为比较好的方法,告诉程序去哪里加载qml

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.addImportPath("C:\\Qt\\Qt5.6.2\\5.6\\static-vs2015-qt5.6.2\\qml");
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

参考方案

还有一种是这样导入,在qml文件夹中找到模块的描述文件qmldir ,根据里面的module classname导入

这里写图片描述

 Q_IMPORT_PLUGIN(classname) //使用类名
qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_module().instance())->registerTypes("QtQuick"); //使用带类名的函数和 模块名字符串

 qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance()) ->initializeEngine( &engine, "QtQuick"); 
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;


    Q_IMPORT_PLUGIN(QtQuick2Plugin) //使用类名
    Q_IMPORT_PLUGIN(QtQuickControlsPlugin) //使用类名
    Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
    Q_IMPORT_PLUGIN(QtQuickLayoutsPlugin)
    Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)

    ////////
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance())->registerTypes("QtQuick"); //使用带类名的函数和 模块名字符串

    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance()) ->initializeEngine( &engine, "QtQuick"); //使用带类名的函数和 模块名字符串

    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls"); //使用带类名的函数和 模块名字符串

    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin.instance()) ->initializeEngine( &engine, "QtQuick.Controls"); //使用带类名的函数和 模块名字符串



    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance())->registerTypes("QtQuick");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls.Private");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->registerTypes("QtQuick.Controls.Styles");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->registerTypes("QtQuick.Layouts");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2WindowPlugin().instance()) ->registerTypes("QtQuick.Window.2");


    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance()) ->initializeEngine( &engine, "QtQuick");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickControlsPlugin().instance())->initializeEngine( &engine, "QtQuick.Controls");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Layouts");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuickLayoutsPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Layouts");
    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2WindowPlugin().instance()) ->initializeEngine( &engine, "QtQuick.Window.2");



    //engine.addImportPath("C:\\Qt\\Qt5.6.2\\5.6\\static-vs2015-qt5.6.2\\qml");

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

引用

http://doc.qt.io/qt-5/qtqml-syntax-imports.html#qml-import-path
http://tieba.baidu.com/p/3537364638?share=9105&fr=share

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值