-2

Given a folder and subfolders all containing jpeg files, what function can I write an function to pull a random file from it, every time the function is called.

For example say we have the follwing directory structure:

root_directory/
    ├── image_1.jpg
    ├── child_directory_1/
    │   ├── image_1.jpg
    │   └── image_2.jpg
    └── child_direcory_2/
        ├── child_directory_2_1/
        │   └── image_1.jpg
        └── child_directory_2_1/
            └── image_1.jpg

Is there a way to open a random image when the location of root_directory is specified.

glamis
  • 85
  • 9

1 Answers1

2

my suggestion would be, use the qdirEntryList:

  1. set the root to root_directory
  2. then search all the images using a filter(png, jpg or what you need) and
  3. after that, generate a pseudo random number between 0 and list.size()
  4. and use that as index for getting the path of the image,
  5. that finally you will use in the constructor of the QImage Object you are going to return.
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97