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

阿尔卑斯汉乔格鲁

我有以下目录结构:

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

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

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')
阿尔卑斯汉乔格鲁

正如@eyllanesc建议的那样,QQuickView可以代替QQmlApplicationEngine起作用:

#include <QGuiApplication>
#include <QQuickView>

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

  QQuickView* view{new QQuickView};
  view->setSource(QUrl("qrc:///qml/main_window_presenter.qml"));
  view->show();

  return app.exec();
}

如果错误消息不是通过说“ MyPresenter不是一个类型”来表明未找到该类型,则可能是我自己发现的这使我相信这是一个参考问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章