Problem with QDir path to the folders
-
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 0x80070002I'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-BitHere'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)); } }
-
@JonB Ok, thanks anyway, at least you gave me a direction :)
@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)));
?
-
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? -
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?@JonB Yes, you right, it's prints only file names without directory path
-
@JonB Yes, you right, it's prints only file names without directory path
@Nick-Shapper
So there are many ways of making the path absolute. If you can't find a better one, you can always prependpath
to eachfilepath
in yourforeach
loop. -
@Nick-Shapper
So there are many ways of making the path absolute. If you can't find a better one, you can always prependpath
to eachfilepath
in yourforeach
loop.@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
-
@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
@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.
-
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 -
@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
@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. -
@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.@JonB Ok, thanks anyway, at least you gave me a direction :)
-
@JonB Ok, thanks anyway, at least you gave me a direction :)
@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)));
?
-
@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)));
?
@JonB I got it working, Thanks a lot you man, really appreciate your help!