Results 1 to 2 of 2

Thread: Multiple Files

  1. #1
    Join Date
    Jan 2018
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Multiple Files

    How we can open multiple files(images) in Qt. I have written a code but this only opens a single image. Here is the Qt code.

    Qt Code:
    1. QFileDialog dialog(this);
    2. dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
    3. dialog.setViewMode(QFileDialog::Detail);
    4. dialog.setFileMode(QFileDialog::ExistingFiles);
    5. QString fileName = QFileDialog::getOpenFileName(this,
    6. tr("Open Images"), "C:/Users/hamza/Desktop/New folder", tr("Image Files (*.png *.jpg *.bmp);;All Files (*.*)"));
    7. if (dialog.exec())
    8. fileName=dialog.selectedFiles();
    9. if (!fileName.isEmpty())
    10. {
    11. QImage image(fileName);
    12. image = image.scaledToWidth(ui->label_pic->width(),Qt::SmoothTransformation);
    13. ui->label_pic->setPixmap(QPixmap::fromImage(image));
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 9th January 2018 at 11:03. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multiple Files

    Why are you using the static method QFileDialog::getOpenFileName() at the same time you are constructing a QFileDialog instance and using the non-static QFileDialog::exec() method? You either do it one way or the other, not both at the same time.

    Qt Code:
    1. QList< QImage > images;
    2.  
    3. QFileDialog dialog(this);
    4. dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
    5. dialog.setViewMode(QFileDialog::Detail);
    6. dialog.setFileMode(QFileDialog::ExistingFiles);
    7.  
    8. if ( QDialog::Accepted == dialog.exec() )
    9. {
    10. QStringList filenames = dialog.selectedFiles();
    11. QStringList::const_iterator it = filenames.begin();
    12. QStringList::const_iterator eIt = filenames.end();
    13. while ( it != eIt )
    14. {
    15. QString fileName = *it++;
    16. if ( !fileName.isEmpty() )
    17. {
    18. QImage image;
    19. if ( image.load( fileName ) )
    20. images.push_back( image );
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    At the end of this piece of code, the QList< QImage > contains all of the images that could be opened successfully. It's up to you to write the UI that will display each of them. The code you posted above will only display a single image on a single QLabel. You'll have to fix that code.

    You could look at the Flow Layout example for ideas. Take the Window.cpp file from that example, and instead of adding QPushButton instances to the flow layout, you could re-write the constructor to take your list of images:

    Qt Code:
    1. #include "flowlayout.h"
    2. #include "window.h"
    3.  
    4. Window::Window( const QList< QImage > & images )
    5. {
    6. FlowLayout *flowLayout = new FlowLayout;
    7.  
    8. QList< QImage >::const_iterator it = images.begin();
    9. QList< QImage >::const_iterator eIt = images.end();
    10. while ( it != eIt )
    11. {
    12. const QImage & image = *it++;
    13. QLabel * pLabel = new QLabel( this );
    14. pLabel->setPixmap( QPixmap::fromImage( image ) );
    15. flowLayout->addWidget( pLabel );
    16. }
    17.  
    18. setLayout(flowLayout);
    19. setWindowTitle(tr("Images in a Flow Layout"));
    20. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Multiple UI files
    By Diblye in forum Newbie
    Replies: 5
    Last Post: 29th June 2012, 01:24
  2. QFtp get multiple files?
    By hexie in forum Qt Programming
    Replies: 0
    Last Post: 12th March 2012, 09:24
  3. Qt Multiple Ui files.....
    By Rajeshsan in forum Qt Programming
    Replies: 4
    Last Post: 19th January 2010, 05:14
  4. example project with multiple .ui files
    By navid in forum Newbie
    Replies: 1
    Last Post: 31st October 2009, 19:25
  5. Error: Using Multiple files
    By 3nc31 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 10:23

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.