我有以下目录结构:
ui/
|- resources.qrc
|- qml/
|- main_window_presenter.qml
|- MyPresenter.qmlresources.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 "." at main_window_presenter.qml,我会得到
QQmlApplicationEngine failed to load component
file::/qml/main_window_presenter.qml:3 MyPresenter is not a type我想我不应该需要一个import语句,因为它们位于同一个目录中。我正在使用介子构建系统,在meson.build中使用这个相关部分(exe_moc_headers在前面定义):
qt5_module = import('qt5')
exe_processed = qt5_module.preprocess(moc_headers : exe_moc_headers, qresources : 'ui/resources.qrc')发布于 2018-07-20 15:05:53
正如@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不是类型“而找不到该类型,我可能自己就知道了。这使我相信,这是一个参考问题。
https://stackoverflow.com/questions/51443466
复制相似问题