Results 1 to 5 of 5

Thread: QStringList, how to access elements after getOpenFileNames

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QStringList, how to access elements after getOpenFileNames

    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

  2. #2
    Join Date
    Feb 2009
    Location
    Lexington, KY
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QStringList, how to access elements after getOpenFileNames

    Quote Originally Posted by ketest View Post
    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:
    Qt Code:
    1. QString fn = fileName.at(i)
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringList, how to access elements after getOpenFileNames

    Or you can use

    Qt Code:
    1. QStringList list = QFileDialog::getOpenFileNames(...);
    2. foreach (QString s, list) {
    3. // do some useful stuff here
    4. qDebug() << "Working on file " << s;
    5. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QStringList, how to access elements after getOpenFileNames

    Quote Originally Posted by franz View Post
    Or you can use

    Qt Code:
    1. QStringList list = QFileDialog::getOpenFileNames(...);
    2. foreach (QString s, list) {
    3. // do some useful stuff here
    4. qDebug() << "Working on file " << s;
    5. }
    To copy to clipboard, switch view to plain text mode 

    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 tests.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Aug 2009
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringList, how to access elements after getOpenFileNames

    The .at was what I was missing.

    Thanks,
    Kevin

Similar Threads

  1. Cutting Elements QStringlist
    By codeman in forum Qt Programming
    Replies: 3
    Last Post: 5th June 2009, 11:19

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.