Hi,
What I’m trying to do is open a PDF file using the user’s default program for this type of files. The way I have it now works, but I don’t think this is the most efficient way of doing it since I’m using the entryList() function and I’m not sure if this is efficient for the purpose I’m using it .
This is what I have. When a button is clicked it looks for a fileName.pdf if this returns false it gets a list of folders in that directory and then iterate trough all of them until it finds fileName.pdf, once found it stops iterating.
void MainWindow::on_pushButton_clicked()
{
bool targetFile = QDesktopServices::openUrl(QUrl("file:///c:/rootFolder/fileName.pdf"));
if(!targetFile)
{
QDir currentDirectory("c:/rootFolder");
QStringList listOfFolders = currentDirectory.entryList(QDir::Dirs);// get a list of folders
for(int i=0; i<listOfFolders.count(); i++)
{
bool targetFileContinued = QDesktopServices::openUrl(QUrl("file:///c:/rootFolder/"+ listOfFolders.at(i)+"/fileName.pdf"));
if(targetFileContinued == true)
{
break;
}
}
}
}
Again, the way I have it works but I have a feeling that this is not what I should be doing in fact I want to go one step deeper (one more directory deeper) if not found in the second attempt which means I will have to get a list of each folder in the first list and iterate trough each folder and so on.
What is the typical way to iterate though multiple folder when searching for a file?
FYI
The URLs are going to be more dynamic than what I show in my code, I just did it like that to show my code.
Thanks a lot
↧