PDA

View Full Version : QStringList, how to access elements after getOpenFileNames



ketest
5th August 2009, 18:32
How do I access the individual strings in a QStringList that was created using getOpenFileNames?

QStringList fileNames;
fileNames = QFileDialog::getOpenFileNames(this,"Select File(s) to search","/home");

// If multiple files names were selected during getOpenFileNames, then how would I print (or access) each one selected?

// I have used getExistingDirectory to get a selected directory and then display it in the textEdit window.
// How can I do the same thing using getOpenFileNames?

QString DirName;
DirName = QFileDialog::getExistingDirectory(this,"Select Folder (Directory) to search in","/home");
ui.textEdit_SearchInFilesFolders->append(DirName);

Thanks,
Kevin

wrdieter
5th August 2009, 19:02
How do I access the individual strings in a QStringList that was created using getOpenFileNames?

QStringList fileNames;
fileNames = QFileDialog::getOpenFileNames(this,"Select File(s) to search","/home");



To assign the ith filename to fn:

QString fn = fileName.at(i)

You can write a straightforward for loop to iterate through the QStringList. The number of elements in the fileNames QStringList will be fileNames.size().

Bill.

franz
5th August 2009, 21:38
Or you can use


QStringList list = QFileDialog::getOpenFileNames(...);
foreach (QString s, list) {
// do some useful stuff here
qDebug() << "Working on file " << s;
}

provided you have keywords enabled. The foreach functionality will be available as Q_FOREACH in any case.

ketest
5th August 2009, 21:47
The .at was what I was missing.

Thanks,
Kevin

spirit
6th August 2009, 06:12
Or you can use


QStringList list = QFileDialog::getOpenFileNames(...);
foreach (QString s, list) {
// do some useful stuff here
qDebug() << "Working on file " << s;
}

provided you have keywords enabled. The foreach functionality will be available as Q_FOREACH in any case.
offtop, it's better to use const refs in foreach, you can take a look on this (http://labs.trolltech.com/blogs/2009/01/23/iterating-efficiently/) tests. :)