PDA

View Full Version : getting filenames



ht1
29th November 2007, 20:51
I am new to file handling with Qt but this


filenametext = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::home().dirName(),
tr("Image Files (*.png *.jpg *.bmp *.gif *.tiff)"));

allows me to open the folder and it displays the filenames. So, it's pretty easy to select one filename and open the file. That's all good but what I need to do is to extract all the filenames from the folder that show. I'm suspecting that QStringList might be the way to go but I can't connect the two points. Could someone suggest to me how to get those filenames and put them into a string (or perhaps write into a file, whichever is easier).
I appreciate your help!

jpn
29th November 2007, 20:57
Hi, take a look at QDir::entryList().

ht1
29th November 2007, 21:35
Thanks jpn.

I can use this to open files


QFileDialog dialog(this);
dialog.setFilter(tr("Images (*.png *.xpm *.jpg)"));
QStringList fileNames;
if (dialog.exec()) fileNames = dialog.selectedFiles();


But how to connect it to creating of the file list? I somehow need to specify what the directory is and I thought the above code did that.
So this code below is unable to create the list.


QStringList list;
list QDir::entryList(QDir::Files, QDir::Name);

jacek
29th November 2007, 21:40
QDir::entryList() isn't static. You need a QDir object to use it.

ht1
29th November 2007, 21:46
Thank you Jacek, but what does it mean exactly.
Would you be able to give me a very simple example how it works.
I just need to generate a list with filenames in a given directory.

Thank you!

jacek
29th November 2007, 21:49
QDir dir( ... );
QStringList list = dir.entryList( QDir::Files, QDir::Name );
You just have to check QDir docs and replace "..." with proper piece of code.

ht1
29th November 2007, 22:32
Thanks jacek, this worked great for me.

The other question I had was, how to get the absolute file path without any filenames (as in C:/folder1/folder2/folder3/) out of QFileDialog as I do this:



filenametext = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::home().dirName(), tr("Image Files (*.png *.jpg)"));


QString path = QDir::absolutePath(); will somehow need to make connection with the "filenametext".

jacek
29th November 2007, 22:35
how to get the absolute file path without any filenames (as in C:/folder1/folder2/folder3/) out of QFileDialog
You need QFileInfo for that. It's quite similar to QDir, but it represents meta-data for a file.

jacek
29th November 2007, 22:47
how to get the absolute file path without any filenames (as in C:/folder1/folder2/folder3/) out of QFileDialog
One more thing: see QFileDialog::getExistingDirectory().

ht1
29th November 2007, 23:15
Jacek, I'm bothering you one more time because solving this problem will allow me to get to something really exciting that I already know how to do.

So, first I want to be able click on a file name and open the file (in directory A). That's done. The code below can already do that.
Second, I want to create a list of all the files that are in this directory (A). This I still can't do, because I can't get the absolute path (without the actual filename attached) of the file I just opened.
What's wrong with the below code? Thanks.



filenametext = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::home().dirName(), tr("Image Files (*.png *.jpg)"));

QFileInfo filenametext;
QDir path = filenametext.absoluteDir();

QDir dir(path);
QStringList list = dir.entryList( QDir::Files, QDir::Name );

jacek
29th November 2007, 23:24
filenametext = QFileDialog::getOpenFileName(...);
QFileInfo filenametext;
QDir path = filenametext.absoluteDir();
You create another "filenametext" variable that covers the previous one. You have to create QFileInfo under some other identifier:

filenametext = QFileDialog::getOpenFileName(...);
QFileInfo fileInfo( <something> );
QDir path = fileInfo.absoluteDir(); // or QDir path( fileInfo.absoluteDir() );

ht1
29th November 2007, 23:51
Hi Jacek,

Thank you so much for your help and patience with me. Everything works fine. I have no more questions. Just for completeness I'm showing the final working code (with filters) in case someone else also wants to do what you helped me figure out.




filenametext = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::home().dirName(),
tr("Image Files (*.png *.jpg *.bmp *.gif *.tiff)"));

QFileInfo fileInfo;
QDir path = fileInfo.absoluteDir();

QDir dir(path);
QStringList list;
list <<"*.png"<<"*.jpg"<<"*.bmp"<<"*.gif"<<"*.tiff";
dir.setNameFilters(list);
list = dir.entryList( QDir::Files, QDir::Name );

jacek
29th November 2007, 23:57
Maybe it will be easier if you use QFileDialog::getOpenFileNames()? The user will have a chance to choose images he wants.

ht1
30th November 2007, 00:30
I guess I have one more question (never enough :)).

In the above code (2 postings back), if I wanted to replace the filenames in "list" with absolute paths + filename, how would I do that? Is there a convenient filter for that?

In other words, instead of getting "filename1.jpg" I want to get "c:/myfolder/filename1.jpg".

Thanks.

jacek
30th November 2007, 00:41
QDir dir( path );
dir.setNameFilters( QStringList() << "*.png" << "*.jpg" << "*.bmp" << "*.gif" << "*.tiff" );

QStringList list;
QFileInfoList fiList( dir.entryInfoList( QDir::Files, QDir::Name ) );
foreach( const QFileInfo & fi, fiList ) {
list << fi.absoluteFilePath();
}

ht1
30th November 2007, 00:49
Amazing!

Thanks a lot, Jacek :)

wyxknouth
10th December 2007, 17:24
Hello i have a problem with that source.
i was develop a simple program that use it my method is this

void mainGUI::fuentes()
{
QStringList files;
QDir directory= QDir(QDir::currentPath());
files = directory.entryList(QStringList("*.ttf"),
QDir::Files );
comboBox->addItems(files); }

This method work online when i run the program in KDevelop or by Konsole but when i go to run by Konqueror or other windows manager it doesnt work the program doesnt show the filename in the combobox.
Thanks

jpn
10th December 2007, 18:20
This method work online when i run the program in KDevelop or by Konsole but when i go to run by Konqueror or other windows manager it doesnt work the program doesnt show the filename in the combobox.
Take a look at http://wiki.qtcentre.org/index.php?title=Current_working_directory.

wyxknouth
10th December 2007, 18:34
Take a look at http://wiki.qtcentre.org/index.php?title=Current_working_directory.
Thanks a lot jpn, it work now.