Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QDir path to the folders

Problem with QDir path to the folders

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.1k Views
  • 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.
  • N Offline
    N Offline
    Nick Shapper
    wrote on last edited by Nick Shapper
    #1

    Hello guys. So basically i want to make a button with which you can add all mp3 files from the folder just in a few clicks. And everything working except one thing.
    When i add the first folder, all files are playing and everything good. BUT when i add the second one, all files from the previous one just cannot be played. They are all just being skipped to the first file from the second folder. And if i add the first folder again, the same happens to the second one and on and on. It's also showing the following errors: DirectShowPlayerService::doSetUrlSource: Unresolved error code 0x80070005
    DirectShowPlayerService::doSetUrlSource: Unresolved error code 0x80070002

    I'm pretty sure it's all because it setting a program's working directory just once and forgets the previous one. I'm a total noob when it comes to directories cause i've never dealt with it. Help me out

    Qt 5.15.2
    Qt Creator 4.14.2
    MinGW 8.1 32-Bit

    Here's the code

    void MainWindow::dirButtonPressed()
    {
        QString folders = QFileDialog::getExistingDirectory(this, "Choose a directory", QString(), QFileDialog::ShowDirsOnly);
    
        QDir directory = folders;
        QString path = directory.absolutePath();
        directory.setCurrent(path); // Pretty sure problem is here
        directory.setFilter(QDir::Files | QDir::NoDotAndDotDot);
    
        QStringList files = directory.entryList();
    
        foreach(QString filepath, files ) {
            QList<QStandardItem *> items;
            items.append(new QStandardItem(QDir(filepath).dirName()));
            standardPlaylistModel->appendRow(items);
            mediaPlaylist->addMedia(QUrl(filepath));
        }
    
    }
    
    1 Reply Last reply
    0
    • N Nick Shapper

      @JonB Ok, thanks anyway, at least you gave me a direction :)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #10

      @Nick-Shapper
      This isn't the best, but kludged:

      mediaPlaylist->addMedia(QUrl(filepath.prepend("/").prepend(path)));
      // or
      mediaPlaylist->addMedia(QUrl(path + QString("/") + filepath));
      // or
      mediaPlaylist->addMedia(QUrl(QString("%1/%2").arg(path).arg(filepath)));
      

      ?

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

        Not sure I understand all of your explanation. But I think

        QStringList files = directory.entryList();
        

        will be getting the file names as relative to the directory you set in directory.setCurrent(path);, i.e. plain names with no leading directory path (print them out to see this)? But your media player won't know that. Pass absolute paths to it, not plain file names?

        N 1 Reply Last reply
        0
        • JonBJ JonB

          Not sure I understand all of your explanation. But I think

          QStringList files = directory.entryList();
          

          will be getting the file names as relative to the directory you set in directory.setCurrent(path);, i.e. plain names with no leading directory path (print them out to see this)? But your media player won't know that. Pass absolute paths to it, not plain file names?

          N Offline
          N Offline
          Nick Shapper
          wrote on last edited by
          #3

          @JonB Yes, you right, it's prints only file names without directory path

          JonBJ 1 Reply Last reply
          0
          • N Nick Shapper

            @JonB Yes, you right, it's prints only file names without directory path

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Nick-Shapper
            So there are many ways of making the path absolute. If you can't find a better one, you can always prepend path to each filepath in your foreach loop.

            N 1 Reply Last reply
            0
            • JonBJ JonB

              @Nick-Shapper
              So there are many ways of making the path absolute. If you can't find a better one, you can always prepend path to each filepath in your foreach loop.

              N Offline
              N Offline
              Nick Shapper
              wrote on last edited by Nick Shapper
              #5

              @JonB Ok, i guess i did something wrong. I changed

              mediaPlaylist->addMedia(QUrl(filepath));
              

              to

              mediaPlaylist->addMedia(QUrl(filepath.prepend(path)));
              

              Now it can't even start to play any file.
              Also i printed out my filepath and it says this:
              "C:/Users/Archivarius-cat/MusicMick Gordon - Bfg Division.mp3"
              "C:/Users/Archivarius-cat/MusicMick Gordon - Damnation.mp3"
              "C:/Users/Archivarius-cat/MusicMick Gordon - Uac Report File Shto36u3.mp3"

              from what i can see there's no / after Music folder, and it's weird. I'm gonna remove prepend function and see what it's gonna print without it

              JoeCFDJ JonBJ 2 Replies Last reply
              0
              • N Nick Shapper

                @JonB Ok, i guess i did something wrong. I changed

                mediaPlaylist->addMedia(QUrl(filepath));
                

                to

                mediaPlaylist->addMedia(QUrl(filepath.prepend(path)));
                

                Now it can't even start to play any file.
                Also i printed out my filepath and it says this:
                "C:/Users/Archivarius-cat/MusicMick Gordon - Bfg Division.mp3"
                "C:/Users/Archivarius-cat/MusicMick Gordon - Damnation.mp3"
                "C:/Users/Archivarius-cat/MusicMick Gordon - Uac Report File Shto36u3.mp3"

                from what i can see there's no / after Music folder, and it's weird. I'm gonna remove prepend function and see what it's gonna print without it

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #6

                @Nick-Shapper addMedia( QUrl::fromLocalFile( m_fileName ) ) <==add them one after another with full path. Normally you need a filter to extract all files which can be played.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  Nick Shapper
                  wrote on last edited by
                  #7

                  No, when i removed it it printed:
                  "Mick Gordon - Bfg Division.mp3"
                  "Mick Gordon - Damnation.mp3"
                  "Mick Gordon - Uac Report File Shto36u3.mp3
                  So i guess it was working

                  1 Reply Last reply
                  0
                  • N Nick Shapper

                    @JonB Ok, i guess i did something wrong. I changed

                    mediaPlaylist->addMedia(QUrl(filepath));
                    

                    to

                    mediaPlaylist->addMedia(QUrl(filepath.prepend(path)));
                    

                    Now it can't even start to play any file.
                    Also i printed out my filepath and it says this:
                    "C:/Users/Archivarius-cat/MusicMick Gordon - Bfg Division.mp3"
                    "C:/Users/Archivarius-cat/MusicMick Gordon - Damnation.mp3"
                    "C:/Users/Archivarius-cat/MusicMick Gordon - Uac Report File Shto36u3.mp3"

                    from what i can see there's no / after Music folder, and it's weird. I'm gonna remove prepend function and see what it's gonna print without it

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @Nick-Shapper said in Problem with QDir path to the folders:

                    from what i can see there's no / after Music folder, and it's weird. I'm gonna remove prepend function and see what it's gonna print without it

                    When I said "prepend", I meant metaphorically! I didn't even know there was a QString::prepend() method. Yes you have to put the directory-separator-slash in. There are many ways to do path manipulation in Qt, sorry I don't have time to look them up for you.

                    N 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Nick-Shapper said in Problem with QDir path to the folders:

                      from what i can see there's no / after Music folder, and it's weird. I'm gonna remove prepend function and see what it's gonna print without it

                      When I said "prepend", I meant metaphorically! I didn't even know there was a QString::prepend() method. Yes you have to put the directory-separator-slash in. There are many ways to do path manipulation in Qt, sorry I don't have time to look them up for you.

                      N Offline
                      N Offline
                      Nick Shapper
                      wrote on last edited by
                      #9

                      @JonB Ok, thanks anyway, at least you gave me a direction :)

                      JonBJ 1 Reply Last reply
                      0
                      • N Nick Shapper

                        @JonB Ok, thanks anyway, at least you gave me a direction :)

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @Nick-Shapper
                        This isn't the best, but kludged:

                        mediaPlaylist->addMedia(QUrl(filepath.prepend("/").prepend(path)));
                        // or
                        mediaPlaylist->addMedia(QUrl(path + QString("/") + filepath));
                        // or
                        mediaPlaylist->addMedia(QUrl(QString("%1/%2").arg(path).arg(filepath)));
                        

                        ?

                        N 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @Nick-Shapper
                          This isn't the best, but kludged:

                          mediaPlaylist->addMedia(QUrl(filepath.prepend("/").prepend(path)));
                          // or
                          mediaPlaylist->addMedia(QUrl(path + QString("/") + filepath));
                          // or
                          mediaPlaylist->addMedia(QUrl(QString("%1/%2").arg(path).arg(filepath)));
                          

                          ?

                          N Offline
                          N Offline
                          Nick Shapper
                          wrote on last edited by
                          #11

                          @JonB I got it working, Thanks a lot you man, really appreciate your help!

                          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