如何在QML文件中导入QML组件资源

时间:2018-07-20 13:19:24

标签: qt qml qt5 qqmlapplicationengine

我具有以下目录结构:

ui/
  |- resources.qrc
  |- qml/
    |- main_window_presenter.qml
    |- MyPresenter.qml

resources.qrc内容:

<RCC>
    <qresource prefix="/">
        <file>qml/MyPresenter.qml</file>
        <file>qml/main_window_presenter.qml</file>
    </qresource>
</RCC>

MyPresenter.qml内容:

import QtQuick 2.11

FocusScope {
  id: root

  property Item view
  property QtObject model

  Component.onCompleted: {
    root.view.anchors.fill = root
    root.view.focus = true
  }
}

main_window_presenter.qml内容:

import "."

MyPresenter {
  id: root
}

main.cpp内容:

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char **argv)
{
  QGuiApplication app(argc, argv);

  QQmlApplicationEngine engine;
  engine.load(":/qml/main_window_presenter.qml");

  return app.exec();
}

运行应用程序时,我会得到

QQmlApplicationEngine failed to load component
file::/qml/main_window_presenter.qml:1 import "." has no qmldir and no namespace

如果我在main_window_presenter.qml上删除import ".",我会得到

QQmlApplicationEngine failed to load component                                                                                                                             
file::/qml/main_window_presenter.qml:3 MyPresenter is not a type

我认为我不需要导入语句,因为它们位于同一目录中。我正在使用介子构建系统以及meson.build中的这一相关部分(exe_moc_headers之前已定义):

qt5_module = import('qt5')
exe_processed = qt5_module.preprocess(moc_headers : exe_moc_headers, qresources : 'ui/resources.qrc')

1 个答案:

答案 0 :(得分:1)

@eyllanesc建议使用QQuickView代替QQmlApplicationEngine:

pecl install sqlsrv-3.0.1    
downloading sqlsrv-3.0.1.tgz ...
Starting to download sqlsrv-3.0.1.tgz (134,966 bytes)
.............................done: 134,966 bytes
17 source files, building
running: phpize
Cannot find config.m4.
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

ERROR: `phpize' failed

如果错误消息没有指出未找到该类型,我可能自己想通了,方法是说“ MyPresenter不是类型” 。这使我相信这是一个参考问题。

相关问题