Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Question to Qt.resolvedUrl () on Windows

Question to Qt.resolvedUrl () on Windows

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Padlock
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Padlock
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Padlock
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        1
        • P Padlock

          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

          M Offline
          M Offline
          Mairtin
          wrote on last edited by Mairtin
          #4

          Only 4 years late with the response.
          Might be a Qt6 thing, but It's file: not file://.

          Qt.resolvedUrl("file:" + model.filePath)

          Result:

          file:///c:/proj/mypro/.....

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved