QML Svg in qt 5.15 static build, using CMake

Viewed 1113

I would like to migrate from 5.9 to 5.15 and I don't get to load svg from QML. The original version, using qt provided by Ubuntu (5.9) did load svg from QML with no extra steps. The new build uses a static Qt.

I use:

  • Ubuntu 18
  • In the house static build of Qt 5.15
  • CMake (3.18)

build Qt

 ./configure \
    -confirm-license \
    -release \
    -static \
    -no-pch \
    -nomake tests \
    -nomake examples \
    -nomake tools \
    -nomake examples \
    -nomake tests \
    -skip qtdoc \
    -skip wayland \
    -skip qtwebengine \
    --prefix=/opt/Qt-5.15.0

CMake

cmake_minimum_required(VERSION 3.18)
project(test VERSION 0.0.0 LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt5 5.15 
    PATHS /opt/Qt-5.15.0
    COMPONENTS
        Core
        Widgets
        Qml
        Svg
        Quick
        QuickControls2
        Charts
        Multimedia
        LinguistTools
        QmlImportScanner 
        Xml
    REQUIRED QUIET
)

add_executable(test_qt

    # c++ code files
    main.cpp

    # qml code files
    qml/Main.qml

    # resource files
    qml/resources.qrc
)

target_include_directories(test_qt PRIVATE ${CMAKE_CURRENT_LIST_DIR})

target_link_libraries(test_qt 
    PRIVATE
        Qt5::Widgets
        Qt5::Qml
        Qt5::Quick
        Qt5::QuickControls2
        Qt5::Svg
        Qt5::Xml
        Qt5::Gui
)

qt_import_qml_plugins(test_qt
    INCLUDE 
        Qt5::QtQuick2Plugin
        Qt5::QSvgPlugin
        Qt5::QtQuickControls2Plugin
)

main.cpp

#include <QGuiApplication>
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlContext>

int main(int argc, char *argv[]) {
    int status = 0;

    try {

        QGuiApplication       app(argc, argv);
        QQmlApplicationEngine engine;

        engine.load(QUrl("qrc:/Main.qml"));

        QObject::connect(&engine, &QQmlApplicationEngine::quit, &QGuiApplication::quit);

        if (engine.rootObjects().isEmpty())
            return -1;

        status = app.exec();

    } catch (std::exception &) {
        return -1;
    }

    return status;
}

Main.qml

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    id: window
    width: 850
    height: 850
    visible: true
    color: "lightgray"

    Image{
        source: "image.svg"
    }
}

I omit the resource file, but it lists qml, png and svg files. When using PNG image is loaded successfully, but when using SVG the following error appears: qrc:/Main.qml:13:5: QML Image: Error decoding: qrc:/image.svg: Unsupported image format

The current Qt build contains the following files related to SVG handling:

./lib/libQt5Svg.a
./lib/libQt5Svg.la
./lib/cmake/Qt5Svg
./lib/cmake/Qt5Svg/Qt5SvgConfigVersion.cmake
./lib/cmake/Qt5Svg/Qt5SvgConfig.cmake
./lib/cmake/Qt5Gui/Qt5Gui_QSvgIconPlugin.cmake
./lib/cmake/Qt5Gui/Qt5Gui_QSvgPlugin_Import.cpp
./lib/cmake/Qt5Gui/Qt5Gui_QSvgIconPlugin_Import.cpp
./lib/cmake/Qt5Gui/Qt5Gui_QSvgPlugin.cmake
./lib/pkgconfig/Qt5Svg.pc
./lib/libQt5Svg.prl
./include/QtSvg
./include/QtSvg/QGraphicsSvgItem
./include/QtSvg/5.15.0/QtSvg
./include/QtSvg/QSvgRenderer
./include/QtSvg/QSvgWidget
./include/QtSvg/QSvgGenerator
./include/QtSvg/QtSvg
./include/QtSvg/QtSvgVersion
./include/QtSvg/QtSvgDepends

EDIT

I added the missing Svg library dependency to the find_package command. The issue remains unsolved.

If I link against a shared build of the Qt libraries, this project works correctly.

2 Answers

The solution for me was in addition to including Qt5::Svg in find_package and target_link_libraries, eg:

find_package(Qt5 REQUIRED COMPONENTS Core Quick Qml Xml Charts Gui Svg QuickControls2)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick Qt5::Qml Qt5::Xml Qt5::Charts Qt5::Gui Qt5::Svg Qt5::QuickControls2)

To also include the following when statically linking Qt 5.15:

qt5_import_plugins(${PROJECT_NAME} INCLUDE Qt5::QSvgPlugin)

That resolved errors such as "QML ButtonIcon: Error decoding: qrc:/resources/images/add-white-36dp.svg: Unsupported image format"

You are using cmake, but in a qmake .pro project,using static linking, you must add

QT += svg

Note that svg plugin depends of widgets:

"QT += svg" is the same that "QT += svg widgets"

In android/IOS is better skip svg images to reduce app size.

/opt/Qt/5.15.1/gcc_64/lib$ ldd libQt5Svg.so
    linux-vdso.so.1 (0x00007fff7bff0000)
    libQt5Widgets.so.5 => /opt/Qt/5.15.1/gcc_64/lib/./libQt5Widgets.so.5 (0x00007ff0cbf55000)
    libQt5Gui.so.5 => /opt/Qt/5.15.1/gcc_64/lib/./libQt5Gui.so.5 (0x00007ff0cb624000)

You can try to add some C++ QTSVG code in your main.cpp ,to test linking

https://doc.qt.io/qt-5/qsvggenerator.html

Related