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. When is it appropriate to use "file://" for a QUrl?
Forum Updated to NodeBB v4.3 + New Features

When is it appropriate to use "file://" for a QUrl?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 3.2k 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    We have an object that uses this object to provide SaveAs dialogs for QML:

    class FileSaveDialog : public QQuickItem
    

    Inside the object we define 2 more objects to facilitate this:

    QPlatformFileDialogHelper *m_dlgHelper;
    ...
    QSharedPointer<QFileDialogOptions> m_options;
    

    At some point I get a path:

    C:\\ProgramData\\XM Editor\\RnD\\programs
    

    I set that path on m_options:

    m_options->setInitialDirectory(folderUrl_);
    

    QML is involved in this chain of events. It sets the path of the folderUrl property on our object.

    Problems:

    • If I set this path on the object, which is a QUrl (folderUrl_) then it opens up the dialog to an appropriate place. It however throws a message in the console:
    shellItem : Unhandled scheme:  "c"
    
    • If I append "file://" to the beginning it grinds away and puts a network access error in the console and then finally shows the dialog and the correct path:
    shellItem: SHCreateItemFromParsingName()) failed (The network path was not found.)
    

    *if I append "file:///" to the beginning I don't see any messages in the console and it appears to open to the correct directory.

    So, it seems like I should be using "file://", but by doing so I have to prepend an extra "/" to the directory for Windows systems only. I believe under Linux our directories are absolute so already have a "/". This makes it so I have to have conditional code for Windows systems:

    #ifdef Q_OS_WIN
        emit saveLocalFile("/" + cell_info->progDir(), filename);
    #else
        emit saveLocalFile(cell_info->progDir(), filename);
    #endif
    

    This seems like it should not require this conditional code. I have tried changing the directory to:

    QDir(cell_info->progDir()).canonicalPath()
    ...
    C:/ProgramData/XM Editor/RnD/programs
    

    This gives the same output and results for all 3 cases as with the output and results of the escaped windows string.

    C++ is a perfectly valid school of magic.

    aha_1980A 1 Reply Last reply
    0
    • fcarneyF fcarney

      We have an object that uses this object to provide SaveAs dialogs for QML:

      class FileSaveDialog : public QQuickItem
      

      Inside the object we define 2 more objects to facilitate this:

      QPlatformFileDialogHelper *m_dlgHelper;
      ...
      QSharedPointer<QFileDialogOptions> m_options;
      

      At some point I get a path:

      C:\\ProgramData\\XM Editor\\RnD\\programs
      

      I set that path on m_options:

      m_options->setInitialDirectory(folderUrl_);
      

      QML is involved in this chain of events. It sets the path of the folderUrl property on our object.

      Problems:

      • If I set this path on the object, which is a QUrl (folderUrl_) then it opens up the dialog to an appropriate place. It however throws a message in the console:
      shellItem : Unhandled scheme:  "c"
      
      • If I append "file://" to the beginning it grinds away and puts a network access error in the console and then finally shows the dialog and the correct path:
      shellItem: SHCreateItemFromParsingName()) failed (The network path was not found.)
      

      *if I append "file:///" to the beginning I don't see any messages in the console and it appears to open to the correct directory.

      So, it seems like I should be using "file://", but by doing so I have to prepend an extra "/" to the directory for Windows systems only. I believe under Linux our directories are absolute so already have a "/". This makes it so I have to have conditional code for Windows systems:

      #ifdef Q_OS_WIN
          emit saveLocalFile("/" + cell_info->progDir(), filename);
      #else
          emit saveLocalFile(cell_info->progDir(), filename);
      #endif
      

      This seems like it should not require this conditional code. I have tried changing the directory to:

      QDir(cell_info->progDir()).canonicalPath()
      ...
      C:/ProgramData/XM Editor/RnD/programs
      

      This gives the same output and results for all 3 cases as with the output and results of the escaped windows string.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @fcarney I recently stumbled over the same, and yes, Windows requires 3 slashes after file:

      Have you tried QUrl::fromLocalFile?
      I guess it should handle it correctly.

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @fcarney , @aha_1980
        EDIT I think I was indeed wrong!** I could well be wrong on this :) But I thought I have come across cases where you're not supposed to put a : after the drive letter when inside a file:... protocol? So it should be more like:

        ~~ file:///C/ProgramData/XM Editor/RnD/programs ~~
        

        (Not sure about the /// vs //.) Take my comment with a pinch of salt, throw it over your shoulder if it's nonsense....

        aha_1980A 1 Reply Last reply
        0
        • JonBJ JonB

          @fcarney , @aha_1980
          EDIT I think I was indeed wrong!** I could well be wrong on this :) But I thought I have come across cases where you're not supposed to put a : after the drive letter when inside a file:... protocol? So it should be more like:

          ~~ file:///C/ProgramData/XM Editor/RnD/programs ~~
          

          (Not sure about the /// vs //.) Take my comment with a pinch of salt, throw it over your shoulder if it's nonsense....

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB

          (Not sure about the /// vs //.) Take my comment with a pinch of salt, throw it over your shoulder if it's nonsense....

          Done.

          PS: QUrl::fromLocalFile() really works :)

          Qt has to stay free or it will die.

          1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            @aha_1980 said in When is it appropriate to use "file://" for a QUrl?:

            Have you tried QUrl::fromLocalFile?

            Wow, I used this as a filter:

            QUrl().fromLocalFile(cell_info->progDir()).toString()
            

            It produced:

            file:///C:/ProgramData/XM Editor/RnD/programs
            

            So, yes, just tried it. That should be how I handle url local paths from now on. What a great find, thank you.

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Just in case, QUrl::fromLocalFile is a static method so no need to instantiate a QUrl object to use it.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

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