How to read all files from a selected directory and use them one by one?
-
Hi
My main aim is to open a window to display some images of same size as astack of images
(sequence of images) and place ascroll bar
such that when i move the scroll bar the image is displayed accordingly from the stack.My idea of doing this is store all the image files from the selected
directory
(folder) and use one image at a time to display when i move the scroll bar.I tried something like this
QDir directory = QFileDialog::getExistingDirectory(this, tr("select directory")); list = QDir::Files(directory); fileName = list(1);
but this is a wrong thing. Does anyone developed something of this sort? or anyother ideas how this can be done?
-
Have a look at QDir::entryList()
This returns you a QStringList with the entries in the selected directoryFor Example
//assume the directory exists and contains some files and you want all jpg and JPG files QDir directory("Pictures/MyPictures"); QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.JPG",QDir::Files); foreach(QString filename, images) { //do whatever you need to do }
(Maybe there are better solutions, this one should work too :) )
-
@the_ Thanks a lot, this idea really worked well to store the filenames and use them.
Do you know how to set the maximum(value) for the scroll bar? I want to assign the length of QStringList to be the maximum of scroll bar.
QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.JPG",QDir::Files); int stack = images.length(); QScrollBar.setMaximum(stack);
but i have errors. I want to connect
horizontalSlider
andlabel
(display window) withSLIGNAL and SLOTS
such that when i scroll the slider the image should change.
Any ideas of doing this?one last question:
ifQStringList images
has 10 fileNames, is it possible to access a particular fileName
such asimages(5) or images(9)
?
i am able to access the first and last fileName withimages.first()
orimages.last()
but not the ones which are in between. -
@beginMyCoding found the solution for
one last question: if QStringList images has 10 fileNames, is it possible to access a particular fileName such as images(5) or images(9)? i am able to access the first and last fileName with images.first() or images.last() but not the ones which are in between.
But waiting for the solution to set
maximum
ofhorizontalslider
-
@beginMyCoding Did you tried the at() ? you can get the particular element at a specific position from QStringList using at()
for example , in your code
images.at(index) will give the element