Question to Qt.resolvedUrl () on Windows
-
Hello,
I've got a model that provides the path of an image file.
Image { id: img y: 0 x: 0 width: 75 height: 75 source: model.fileIsDir ? "qrc:folder.png" : Qt.resolvedUrl(model.filePath) onSourceChanged: { console.log ("Image source", model.filePath) } }
The debug window shows:
qml: Image source C:/Users/MyUser/Pictures/IMG_00000001.jpg
and
QML Image: Protocol "c" is unknown
I don't really understand why but I changed the line that contains Qt.resolvedUrl () to
: Qt.resolvedUrl("file://" + model.filePath)
This means:
Image { id: img y: 0 x: 0 width: 75 height: 75 source: model.fileIsDir ? "qrc:folder.png" : Qt.resolvedUrl("file://" + model.filePath) onSourceChanged: { console.log ("Image source", model.filePath) } }
Now I receive
QML Image: Cannot open: file://c/Users/MyUser/Pictures/IMG_00000001.jpg
which makes sense. If I try to open that URL in a browser it won't open it either. There's a colon missing. The URL
file://c:/Users/MyUser/Pictures/IMG_00000001.jpg
opens in a browser and displays the picture.
Is Qt.resolvedUrl () not the correct function to call to convert a file system path into a URL?
-
I think the issue is with the Image {source: ... } property.
I thought I can easily work around this but it doesn't look like it.This:
QString OneFile::fileURL() const { QString qsURL = "file://" + m_FileInfo.filePath (); return qsURL; }
results in the same QML error message:
QML Image: Cannot open: file://c/Users/MyUser/Pictures/IMG_00000001.jpg
Which means that the colon is swalled in QML, somewhere in the Image component.
I also tried this:
QString OneFile::fileURL() const { QString qsURL = m_FileInfo.filePath (); qsURL.replace (":", "%3A"); qsURL = "file://" + qsURL; return qsURL; }
This doesn't cause any QML error message. It simply doesn't work. The image is not displayed.
What am I doing wrong?
-
Got it ;-)
QString OneFile::fileURL() const { return QUrl::fromLocalFile (m_FileInfo.filePath ()).toString (); }
This topic helped me with the solution:
QQuickWidget no such file issue -
Got it ;-)
QString OneFile::fileURL() const { return QUrl::fromLocalFile (m_FileInfo.filePath ()).toString (); }
This topic helped me with the solution:
QQuickWidget no such file issue