如何在 QML 文件中导入 QML 组件资源
how to import a QML Component resource in a QML file
我的目录结构如下:
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')
正如@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" 来指示未找到类型,我可能已经想到了。这让我相信这是一个参考问题。
我的目录结构如下:
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')
正如@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" 来指示未找到类型,我可能已经想到了。这让我相信这是一个参考问题。