当我试图将QtCharts模块导入我的QML文件时,y总是收到相同的警告消息:
发现未工作导入: file: C:/.模块"QtCharts“未安装
我在QT5.7.0中使用OpenSource QtCreator 4.0.3。
路径中有一个文件夹QtCharts:C:\Qt\5.7\mingw53_32\qml
我还使用.pro文件中的属性包含了文件夹路径:
QML2_IMPORT_PATH: C:\Qt\5.7\mingw53_32\qml我遗漏了什么?
下面是一个简单的测试代码:
// QtChartsTest.pro
TEMPLATE = app
QT += qml quick
QT += charts
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML2_IMPORT_PATH = C:\Qt\5.7\mingw53_32\qml
# Default rules for deployment.
include(deployment.pri)// Main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}// Main.qml
import QtQuick 2.7
import QtQuick.Window 2.2
import QtCharts 2.1
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
}发布于 2017-06-16 15:17:54
我被困在了一个类似的问题上,对Qt关于这个主题的文档感到沮丧,所以我将把我自己解决问题的方法放在这里,以供后人使用。
我不知道你的问题的确切原因,但我可以给你一个建议,尝试并排除它。
在您的main.cpp中添加以下行。qDebug()<<engine.importPathList();
所以你的主要目标是
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qDebug()<<engine.importPathList();
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}这将包括包含路径的完整列表。如果没有看到其中列出的包含路径,可以通过添加以下行来添加它:
engine.addImportPath(directory);
其中“目录”是包含目录的QString。
我的理解是,QML2_IMPORT_PATH变量仅在运行时应用,而不是在编译时应用,这意味着QtCreator在编译时看不到路径。另一方面,engine.addImportPath(directory);在启动qml引擎之前添加路径(因此第一个qml导入语句)
我不是说这是解决你问题的最好办法,但它确实帮助我解决了我的问题。
发布于 2020-12-15 04:34:47
int main(int argc, char *argv[])
{
// Qt Charts uses Qt Graphics View Framework for drawing, therefore QApplication must be used.
QApplication app(argc, argv);
QQuickView viewer;
// The following are needed to make examples run without having to install the module
// in desktop environments.
#ifdef Q_OS_WIN
QString extraImportPath(QStringLiteral("%1/../../../../%2"));
#else
QString extraImportPath(QStringLiteral("%1/../../../%2"));
#endif
viewer.engine()->addImportPath(extraImportPath.arg(QGuiApplication::applicationDirPath(),
QString::fromLatin1("qml")));
//***** [Solve] FORCE THE MODULE TO BE IMPORTED.
QObject::connect(viewer.engine(), &QQmlEngine::quit, &viewer, &QWindow::close);
qDebug() << viewer.engine()->importPathList();
viewer.setTitle(QStringLiteral("QML Axes"));
viewer.setSource(QUrl("qrc:/qml/qmlaxes/main.qml"));
viewer.setResizeMode(QQuickView::SizeRootObjectToView);
viewer.show();
return app.exec();
}qrc:/qt-project.org/imports", "/opt/Qt/5.15.2/gcc_64/qml
您可以在内置的"QML轴“示例源中找到答案。
https://stackoverflow.com/questions/38834973
复制相似问题