Results 1 to 19 of 19

Thread: getting filenames

  1. #1
    Join Date
    Nov 2007
    Posts
    47
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34

    Default getting filenames

    I am new to file handling with Qt but this
    Qt Code:
    1. filenametext = QFileDialog::getOpenFileName(this,
    2. tr("Open Image"), QDir::home().dirName(),
    3. tr("Image Files (*.png *.jpg *.bmp *.gif *.tiff)"));
    To copy to clipboard, switch view to plain text mode 
    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!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: getting filenames

    Hi, take a look at QDir::entryList().
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    ht1 (29th November 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    47
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34

    Default Re: getting filenames

    Thanks jpn.

    I can use this to open files
    Qt Code:
    1. QFileDialog dialog(this);
    2. dialog.setFilter(tr("Images (*.png *.xpm *.jpg)"));
    3. QStringList fileNames;
    4. if (dialog.exec()) fileNames = dialog.selectedFiles();
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. list QDir::entryList(QDir::Files, QDir::Name);
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: getting filenames

    QDir::entryList() isn't static. You need a QDir object to use it.

  6. The following user says thank you to jacek for this useful post:

    ht1 (29th November 2007)

  7. #5
    Join Date
    Nov 2007
    Posts
    47
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34

    Default Re: getting filenames

    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!

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: getting filenames

    Qt Code:
    1. QDir dir( ... );
    2. QStringList list = dir.entryList( QDir::Files, QDir::Name );
    To copy to clipboard, switch view to plain text mode 
    You just have to check QDir docs and replace "..." with proper piece of code.

  9. The following user says thank you to jacek for this useful post:

    ht1 (29th November 2007)

  10. #7
    Join Date
    Nov 2007
    Posts
    47
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34

    Default Re: getting filenames

    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:

    Qt Code:
    1. filenametext = QFileDialog::getOpenFileName(this,
    2. tr("Open Image"), QDir::home().dirName(), tr("Image Files (*.png *.jpg)"));
    To copy to clipboard, switch view to plain text mode 

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

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: getting filenames

    Quote Originally Posted by ht1 View Post
    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.

  12. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: getting filenames

    Quote Originally Posted by ht1 View Post
    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().

  13. The following user says thank you to jacek for this useful post:

    ht1 (29th November 2007)

  14. #10
    Join Date
    Nov 2007
    Posts
    47
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34

    Default Re: getting filenames

    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.

    Qt Code:
    1. filenametext = QFileDialog::getOpenFileName(this,
    2. tr("Open Image"), QDir::home().dirName(), tr("Image Files (*.png *.jpg)"));
    3.  
    4. QFileInfo filenametext;
    5. QDir path = filenametext.absoluteDir();
    6.  
    7. QDir dir(path);
    8. QStringList list = dir.entryList( QDir::Files, QDir::Name );
    To copy to clipboard, switch view to plain text mode 

  15. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: getting filenames

    Quote Originally Posted by ht1 View Post
    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:
    Qt Code:
    1. filenametext = QFileDialog::getOpenFileName(...);
    2. QFileInfo fileInfo( <something> );
    3. QDir path = fileInfo.absoluteDir(); // or QDir path( fileInfo.absoluteDir() );
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to jacek for this useful post:

    ht1 (29th November 2007)

  17. #12
    Join Date
    Nov 2007
    Posts
    47
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34

    Default Re: getting filenames

    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.

    Qt Code:
    1. filenametext = QFileDialog::getOpenFileName(this,
    2. tr("Open Image"), QDir::home().dirName(),
    3. tr("Image Files (*.png *.jpg *.bmp *.gif *.tiff)"));
    4.  
    5. QFileInfo fileInfo;
    6. QDir path = fileInfo.absoluteDir();
    7.  
    8. QDir dir(path);
    9. list <<"*.png"<<"*.jpg"<<"*.bmp"<<"*.gif"<<"*.tiff";
    10. dir.setNameFilters(list);
    11. list = dir.entryList( QDir::Files, QDir::Name );
    To copy to clipboard, switch view to plain text mode 

  18. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: getting filenames

    Maybe it will be easier if you use QFileDialog::getOpenFileNames()? The user will have a chance to choose images he wants.

  19. #14
    Join Date
    Nov 2007
    Posts
    47
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34

    Default Re: getting filenames

    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.

  20. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: getting filenames

    Qt Code:
    1. QDir dir( path );
    2. dir.setNameFilters( QStringList() << "*.png" << "*.jpg" << "*.bmp" << "*.gif" << "*.tiff" );
    3.  
    4. QFileInfoList fiList( dir.entryInfoList( QDir::Files, QDir::Name ) );
    5. foreach( const QFileInfo & fi, fiList ) {
    6. list << fi.absoluteFilePath();
    7. }
    To copy to clipboard, switch view to plain text mode 

  21. The following user says thank you to jacek for this useful post:

    ht1 (29th November 2007)

  22. #16
    Join Date
    Nov 2007
    Posts
    47
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34

    Default Thank You!!!

    Amazing!

    Thanks a lot, Jacek

  23. #17
    Join Date
    Dec 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: getting filenames

    Hello i have a problem with that source.
    i was develop a simple program that use it my method is this
    Qt Code:
    1. void mainGUI::fuentes()
    2. {
    3. QStringList files;
    4. QDir directory= QDir(QDir::currentPath());
    5. files = directory.entryList(QStringList("*.ttf"),
    6. QDir::Files );
    7. comboBox->addItems(files); }
    To copy to clipboard, switch view to plain text mode 

    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

  24. #18
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: getting filenames

    Quote Originally Posted by wyxknouth View Post
    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?t...king_directory.
    J-P Nurmi

  25. #19
    Join Date
    Dec 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: getting filenames

    Quote Originally Posted by jpn View Post
    Thanks a lot jpn, it work now.

Similar Threads

  1. Preserving filenames in QProcess
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2007, 22:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.