Getting this error (qt.svg: Cannot open file 'qrc:/images/solar-system-animation.svg', because: No such file or directory)
-
//svgitem.h
#ifndef SVGITEM_H
#define SVGITEM_H#include <QQuickPaintedItem>
#include <QObject>
#include <QSvgRenderer>class SvgItem : public QQuickPaintedItem {
Q_OBJECT
Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)public:
SvgItem(QQuickItem *parent = nullptr);
void paint(QPainter *painter) override;QString source() const; void setSource(const QString &source);
signals:
void sourceChanged();private:
QSvgRenderer *m_renderer;
QString m_source;
};#endif // SVGITEM_H
//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "svgitem.h"int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);QQmlApplicationEngine engine; qmlRegisterType<SvgItem>("com.yourcompany.svgitem", 1, 0, "SvgItem"); 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();
}
#include "svgitem.h"
#include <QPainter>SvgItem::SvgItem(QQuickItem *parent)
: QQuickPaintedItem(parent), m_renderer(new QSvgRenderer(this)) {}void SvgItem::paint(QPainter *painter) {
if (m_renderer->isValid()) {
m_renderer->render(painter);
}
}QString SvgItem::source() const {
return m_source;
}void SvgItem::setSource(const QString &source) {
if (m_source != source) {
m_source = source;
m_renderer->load(source);
emit sourceChanged();
update();
}
}//Background.qml
import QtQuick 2.15
import com.yourcompany.svgitem 1.0Item {
width: 600
height: 400SvgItem { id: background anchors.fill: parent source: "qrc:/images/solar-system-animation.svg" Component.onCompleted: { console.log("==========================?",background.source) } }
}
//main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15Window {
visible: true
width: 1200
height: 600
title: "Blended QML Example"Item { anchors.fill: parent // Background QML Loader { source: "Background.qml" anchors.fill: parent } Loader{ source: "Meter.qml" anchors.fill: parent } }
}
//Meter.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15Rectangle{
width: parent.width
height: parent.height
color: "transparent"
RowLayout {
id: rowLayout
anchors.fill: parentRectangle { id: leftguage width: 300 height: 300 radius: width / 2 color: "white" border.color: "black" } Item{ Layout.fillWidth: true } Rectangle { id: rightguage width: 300 height: 300 radius: width / 2 color: "white" border.color: "black" } }
}
-
@Vikas_one Please format your code properly!
Do you have a resource file and if so does it contain that svg file? -
@Vikas_one
Hi and welcome. Please use the forum's Code tags (</>
toolbar button) when posting blocks of literal code.Are you aware that in
source: "qrc:/images/solar-system-animation.svg"
that means it is accessing a Qt embedded resource, not an actual external file? Have you designed and embedded that file into your QML application, however you do that with QML (I don't use it)? -
@Vikas_one It doesn't look like you're loading this resource file like shown here: https://doc.qt.io/qt-6/resources.html
-
Though that page doesn't show any QML examples as far as I can see.
I recall having to try a few things when I first wanted to do this a few years ago. I don't know if what I arrived at is the "correct" approach, but the format that I found to work in QML and have stuck to ever since involves using a triple forward slash like this:
source: "qrc:///images/my-image.png"