Qt Webassembly 通过柯南包管理器

Qt Webassembly via conan package manager

我有一个非常简单的 Qt (QML) 项目:

CMakeLists.txt :

cmake_minimum_required(VERSION 3.12.2)

project(testtttt VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 COMPONENTS Core Quick REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick REQUIRED)

set(PROJECT_SOURCES
        main.cpp
        qml.qrc
)

qt_add_executable(testtttt
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
)

target_link_libraries(testtttt PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick)

set_target_properties(testtttt PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)

qt_import_qml_plugins(testtttt)
qt_finalize_executable(testtttt)

conanfile.txt :

[requires]
qtbase/6.2.2@qt/everywhere
qtdeclarative/6.2.2@qt/everywhere

main.cpp :

#include <QGuiApplication>
#include <QQmlApplicationEngine>


int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

main.qml :

import QtQuick
import QtQuick.Window

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Rectangle {
        anchors.fill: parent
        color: "red"

        Text {
            anchors.centerIn: parent
            text: "Hello World!"
        }
    }
}

qml.qrc :

<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
    </qresource>
</RCC>

如您所见,它正在从 Qt conan 服务器下载官方 Qt conan 包。 (所以它 使用来自 conan-center-index 服务器的 conan 包。)

我可以使用此脚本在我的 macOS 机器上成功构建并运行它:

build_and_run.sh :

set -e

conan install qtbuildprofiles/6.2.2@qt/everywhere -r qt

conan install -u conanfile.txt --profile=macos-universal-clang --build=never --update --generator=virtualenv -r qt
source activate.sh

mkdir -p build
cd build
echo "cmake ../ -G Ninja -DCMAKE_BUILD_TYPE=Release"
cmake ../ -G Ninja -DCMAKE_BUILD_TYPE=Release
VERBOSE=1 cmake --build . --config Release --clean-first
cd ..

#Run the built app :
./build/testtttt

我也在尝试为 WebAssembly 构建它,使用这个脚本:

buildWasm.sh :

set -e

conan install qtbuildprofiles/6.2.2@qt/everywhere -r qt

mkdir -p wasmBuildMachine
cd wasmBuildMachine
conan install -u ../conanfile.txt --profile=../macos-universal-clang --build=never --update --generator=virtualenv -r qt
source activate.sh
QT_HOST_PATH=$(echo "$Qt6_DIR" | sed "s#/lib/cmake/Qt6##g")
echo "QT_HOST_PATH : $QT_HOST_PATH"
source deactivate.sh
cd ..

conan install -u conanfile.txt --profile=macos-webassembly-x86_64-clang --build=never --update --generator=virtualenv -r qt
source activate.sh

QT_CMAKE=$(echo "$Qt6_DIR" | sed "s#/lib/cmake/Qt6##g")
QT_CMAKE+="/bin/qt-cmake"
echo "QT_CMAKE : $QT_CMAKE"

mkdir -p buildWasm
cd buildWasm
echo "$QT_CMAKE ../ -G Ninja -DCMAKE_BUILD_TYPE=Release -DQT_HOST_PATH=$QT_HOST_PATH"
$QT_CMAKE ../ -G Ninja -DCMAKE_BUILD_TYPE=Release -DQT_HOST_PATH=$QT_HOST_PATH
VERBOSE=1 cmake --build . --config Release --clean-first

构建失败并出现此错误:

-- Could NOT find Qt6Quick (missing: Qt6Quick_DIR)
CMake Error at CMakeLists.txt:15 (find_package):
Found package configuration file:

/Users/myUser/.conan/data/qtbase/6.2.2/qt/everywhere/package/5f6084cf36d2dec9c03d119ccdb11027ce3fa5ba/lib/cmake/Qt6/Qt6Config.cmake

but it set Qt6_FOUND to FALSE so package "Qt6" is considered to be NOT
FOUND. Reason given by package:

Failed to find Qt component "Quick".

Expected Config file at
"/Users/myUser/.conan/data/qtbase/6.2.2/qt/everywhere/package/5f6084cf36d2dec9c03d119ccdb11027ce3fa5ba/lib/cmake/Qt6Quick/Qt6QuickConfig.cmake"
does NOT exist

...确实:QtQuick 在 qtdeclarative conan 文件夹中(/Users/myUser/.conan/data/qtbase/6.2.2/qt/everywhere/package/...),而不在 qtbase 中文件夹。

我该如何解决这个问题?

我正在关注这些文档页面:

https://www.qt.io/blog/installing-qt-via-conan-package-manager

https://wiki.qt.io/Using_Conan_for_Qt6

https://www.qt.io/blog/qt-6-build-system

为了简化事情:我的项目也可以从这里下载(我在上面分享的每个来源):

https://wetransfer.com/downloads/e8717b634b2eff069134b4407f77243c20220202190736/81e9433c52a22081915a7d5d144e2ef420220202190748/a8ec17

更新 : 我刚刚尝试了这个小部件应用程序:Qt/Examples/Qt-6.2.1/widgets/richtext/syntaxhighlighter/.

效果很好,使用与我上面分享的完全相同的 conanfile.txtbuildWasm.sh

这是我为 WebAssembly 编译的示例应用程序的屏幕截图,运行ning 在 Chrome 中:

看来问题出在 QtQuick 模块上... 非常欢迎可能的解决方案想法! 我尝试了很多东西,但到目前为止没有任何效果...

事实证明这是 Qt 方面的错误: https://bugreports.qt.io/browse/QTBUG-94524(引入一个QT_ADDITIONAL_HOST_PACKAGES_PREFIX_PATH变量).

部署该修复程序后,相同的脚本应该可以工作。