1

我有以下目录结构:

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')
4

1 回答 1

1

正如@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 is not a type" 来指示未找到该类型,我自己可能已经想到了。这使我相信这是一个参考问题。

于 2018-07-20T15:05:53.260 回答