1

I have to show image from USB device on my Linux based device. My USB has a folder which has a non-ASCII character. It is replaced by "?" character.

Original path : "file:///tmp/USB6/§§§§/Koala.jpg"

and

QML returns error : "QML Image: Cannot open: file:///tmp/USB6/????/Koala.jpg"

How will my Image be loaded with this path?

Image {
  id: mainImage
  source: iconRole
  asynchronous: true
  sourceSize.width:  screenSize_Width
  sourceSize.height:  screenSize_Height
  fillMode: Image.PreserveAspectCrop
  onSourceChanged: {
     console.log("SOURCE = ",source)
  }
}
  1. Logs I am getting : SOURCE = file:///tmp/USB6/§§§§/Koala.jpg
  2. Error I am getting QML Image: Cannot open: file:///tmp/USB6/????/Koala.jpg.
10
  • I know you suspect the character and it might be the reason, but as you mentioned USB too, do you have permission to open files in there?
    – HMD
    Apr 16, 2018 at 10:18
  • Yes all permissions are there. File paths containing ASCII characters are opening properly. Apr 16, 2018 at 10:23
  • I see, can you please share the part of you code that opens the file?
    – HMD
    Apr 16, 2018 at 10:28
  • If you bind your image as resource (in qrc) what do you get. Apr 16, 2018 at 12:07
  • 1
    Make sure that the usb filesystem is mounted with correct file name encoding, that the qml source is UTF-8 encoded, and that the system locale matches it. Before you even begin working with QML, you should get the time to open correctly using QFile. Apr 16, 2018 at 13:39

2 Answers 2

1

Qt docs about URL:

Additionally, URLs may contain encoded characters using the 'percent-encoding' scheme specified by RFC 3986. These characters will be preserved within properties of type url, to allow QML code to construct precise URL values. An exception to this rule is the preemptive decoding of directory-separator characters ('/') - these characters are decoded to allow the URL to be correctly classified.

For example, a local file containing a '#' character, which would normally be interpreted as the beginning of the URL 'fragment' element, can be accessed by encoding the characters of the file name:

Image { source: encodeURIComponent("/tmp/test#1.png") }

This basic type is provided by the QML language.

2
  • Does it need any locale to be set for the system? Apr 16, 2018 at 12:38
  • @AnupamSrivastava Yes I guessed you can't because it's only mentioned ascii symbols and I think Qt/QML doesn't support what you want to do. I know you can do this with Python using wide characters but I don't think it's possible in Qt.
    – HMD
    Apr 17, 2018 at 1:25
1

It worked for this case.

QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

Put this statement in main.cpp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.