Adding SVG to resource with alias and referencing in QML
-
@SPlatten soooo
first of, I would like to apologise, copy url is actually the correct one :Dsecondly, do use a "tiny" svg it will work:
//main.qml import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Window 2.15 import QtQuick.Layouts 1.15 Window { id: root width: 800 height: 600 visible: true Image{ id:img anchors.fill: parent source: "qrc:/SomeAliasedSVG.svg" } }
rsc.qrc <RCC> <qresource prefix="/"> <file>main.qml</file> <file alias="SomeAliasedSVG.svg">01_01.svg</file> </qresource> </RCC>
//svg file <?xml version="1.0"?> <svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" viewBox="0 0 30 30"> <desc>Example SVG file</desc> <rect x="10" y="10" width="10" height="10" fill="red"/> </svg>
works exactly as expected:
wrote on 23 Sept 2022, 06:36 last edited by SPlattenThis post is deleted! -
@SPlatten 🤷♂️
can you make a minimal compile able example for us ?
-
wrote on 23 Sept 2022, 06:44 last edited by SPlattenThis post is deleted!
-
@SPlatten it's not really compile able, is it ?
I don't have your *pro, nor your main.cpp file, nor the folder structure you claim to have.also giving your Image a width and height does help, because its super tiny without it
-
@SPlatten it's not really compile able, is it ?
I don't have your *pro, nor your main.cpp file, nor the folder structure you claim to have.also giving your Image a width and height does help, because its super tiny without it
wrote on 23 Sept 2022, 07:08 last edited by SPlattenThis post is deleted! -
@SPlatten said in Adding SVG to resource with alias and referencing in QML:
Are they ?
They are relevant to have a compilable example...
-
@SPlatten said in Adding SVG to resource with alias and referencing in QML:
Are they ?
They are relevant to have a compilable example...
wrote on 23 Sept 2022, 07:28 last edited by SPlattenThis post is deleted! -
wrote on 23 Sept 2022, 12:48 last edited by SPlattenThis post is deleted!
-
wrote on 23 Sept 2022, 13:25 last edited by
simply:
source: "/images/stale.svg" -
wrote on 23 Sept 2022, 13:30 last edited byThis post is deleted!
-
wrote on 23 Sept 2022, 13:34 last edited byThis post is deleted!
-
wrote on 23 Sept 2022, 13:37 last edited by SPlattenThis post is deleted!
-
wrote on 23 Sept 2022, 13:46 last edited by JoeCFD
@SPlatten I see. All of my images have alias. Therefore, no qrc: is needed anywhere. It is good you drop it as well without alias.
I still use qrc for qml files since they do not have alias
resources/qml/main.qml: source: "qrc:/qml/MainScreen.qml"
resources/qml/main.qml: source: "qrc:/qml/SplashScreen.qml"
I will try to remove it and let you know.Confirmed: qrc is not needed even when resource files do not have alias. qrc: can be dropped completely.
But not sure why Qt did this. The code with or without qrc: should compile
In Qt6 qml module version numbers are dropped. I am using Qt 5.15.2. -
@SPlatten I see. All of my images have alias. Therefore, no qrc: is needed anywhere. It is good you drop it as well without alias.
I still use qrc for qml files since they do not have alias
resources/qml/main.qml: source: "qrc:/qml/MainScreen.qml"
resources/qml/main.qml: source: "qrc:/qml/SplashScreen.qml"
I will try to remove it and let you know.Confirmed: qrc is not needed even when resource files do not have alias. qrc: can be dropped completely.
But not sure why Qt did this. The code with or without qrc: should compile
In Qt6 qml module version numbers are dropped. I am using Qt 5.15.2.wrote on 23 Sept 2022, 13:52 last edited by SPlattenThis post is deleted! -
wrote on 23 Sept 2022, 13:54 last edited by JoeCFD
@SPlatten what is your Qt version? Is this on Linux?
check the file path in your compiled qrc_images.cpp. All your images are compiled into this file. This is my setting.<RCC> <qresource prefix="/res"> <file alias="background">images/SplashBackground.png</file> </qresource> </RCC> Image { /* background image works */ anchors.centerIn: parent source: "/res/background" width: parent.width height: parent.height }
-
@SPlatten what is your Qt version? Is this on Linux?
check the file path in your compiled qrc_images.cpp. All your images are compiled into this file. This is my setting.<RCC> <qresource prefix="/res"> <file alias="background">images/SplashBackground.png</file> </qresource> </RCC> Image { /* background image works */ anchors.centerIn: parent source: "/res/background" width: parent.width height: parent.height }
wrote on 23 Sept 2022, 13:58 last edited byThis post is deleted! -
wrote on 26 Sept 2022, 07:37 last edited by SPlatten
@JoeCFD , I decided to create a simple application to experiment with the resources. I used the same SVG file. The resource file:
<RCC> <qresource prefix="/"> <file alias="stale.svg">stale.svg</file> <file>mainWindow.qml</file> </qresource> </RCC>
In the mainwindow:
void MainWindow::paintEvent(QPaintEvent *pobjEvt) { Q_UNUSED(pobjEvt); QPainter objPainter(this); QImage* pobjImage(new QImage(":/stale.svg")); bool blnIsNull(pobjImage->isNull()); qDebug() << QString("Image isNull: %1").arg(((blnIsNull == true) ? "Yes" : "No")); objPainter.drawImage(10, 10, *pobjImage); }
This works absolutely fine and the iamge is displayed correctly. I will not try to use this to figure out why the original isn't working.
-
@JoeCFD , I decided to create a simple application to experiment with the resources. I used the same SVG file. The resource file:
<RCC> <qresource prefix="/"> <file alias="stale.svg">stale.svg</file> <file>mainWindow.qml</file> </qresource> </RCC>
In the mainwindow:
void MainWindow::paintEvent(QPaintEvent *pobjEvt) { Q_UNUSED(pobjEvt); QPainter objPainter(this); QImage* pobjImage(new QImage(":/stale.svg")); bool blnIsNull(pobjImage->isNull()); qDebug() << QString("Image isNull: %1").arg(((blnIsNull == true) ? "Yes" : "No")); objPainter.drawImage(10, 10, *pobjImage); }
This works absolutely fine and the iamge is displayed correctly. I will not try to use this to figure out why the original isn't working.
wrote on 26 Sept 2022, 11:52 last edited byThis post is deleted!
22/33