我假设您将 Qt Quick Compiler 称为预编译。最简单的方法就是使用 Qt Quick Compiler 构建整个 Qt Quick Controls 模块。
如果您需要在您的项目中使用它,您可以尝试添加一个包含 Qt Quick Controls 导入的导入。 QQmlEngine::addImportPath() 说:
新添加的路径将在 importPathList() 中排在第一位。
该声明似乎暗示顺序很重要,code 证实了这一点:
QStringList localImportPaths = database->importPathList(QQmlImportDatabase::Local);
// Search local import paths for a matching version
const QStringList qmlDirPaths = QQmlImports::completeQmldirPaths(uri, localImportPaths, vmaj, vmin);
for (const QString &qmldirPath : qmlDirPaths) {
QString absoluteFilePath = typeLoader.absoluteFilePath(qmldirPath);
if (!absoluteFilePath.isEmpty()) {
QString url;
const QStringRef absolutePath = absoluteFilePath.leftRef(absoluteFilePath.lastIndexOf(Slash) + 1);
if (absolutePath.at(0) == Colon)
url = QLatin1String("qrc://") + absolutePath.mid(1);
else
url = QUrl::fromLocalFile(absolutePath.toString()).toString();
QQmlImportDatabase::QmldirCache *cache = new QQmlImportDatabase::QmldirCache;
cache->versionMajor = vmaj;
cache->versionMinor = vmin;
cache->qmldirFilePath = absoluteFilePath;
cache->qmldirPathUrl = url;
cache->next = cacheHead;
database->qmldirCache.insert(uri, cache);
*outQmldirFilePath = absoluteFilePath;
*outQmldirPathUrl = url;
return true;
}
}
您的项目结构可能如下所示:
myproject/
qml/
main.qml
QtQuick/
Controls/
Button.qml
ScrollBar.qml
qmldir
在 main.cpp 中,您将路径设置为 qml 目录(请注意,路径会有所不同,具体取决于您是在进行项目的源代码构建还是影子构建,因此您可以想使用资源文件来简化事情):
engine.addImportPath("path/to/qml");
请注意,控件导入其他类型。例如,Button uses the Settings singleton,它来自 QtQuick.Controls.Private 导入,因此您也需要将其复制到 qml 目录中。 Settings 为按钮加载特定样式 (ButtonStyle),可以是 this folder 中的任何样式,具体取决于所使用的样式。
简而言之,您需要复制您正在使用的 QML 文件的所有潜在依赖项。