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.
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
if ( QDialog::Accepted == dialog.
exec() ) {
while ( it != eIt )
{
if ( !fileName.isEmpty() )
{
if ( image.load( fileName ) )
images.push_back( image );
}
}
}
QList< QImage > images;
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
dialog.setFileMode(QFileDialog::ExistingFiles);
if ( QDialog::Accepted == dialog.exec() )
{
QStringList filenames = dialog.selectedFiles();
QStringList::const_iterator it = filenames.begin();
QStringList::const_iterator eIt = filenames.end();
while ( it != eIt )
{
QString fileName = *it++;
if ( !fileName.isEmpty() )
{
QImage image;
if ( image.load( fileName ) )
images.push_back( image );
}
}
}
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:
#include "flowlayout.h"
#include "window.h"
Window
::Window( const QList<
QImage >
& images
){
FlowLayout *flowLayout = new FlowLayout;
QList<
QImage >
::const_iterator it
= images.
begin();
QList<
QImage >
::const_iterator eIt
= images.
end();
while ( it != eIt )
{
pLabel
->setPixmap
( QPixmap::fromImage( image
) );
flowLayout->addWidget( pLabel );
}
setLayout(flowLayout);
setWindowTitle(tr("Images in a Flow Layout"));
}
#include "flowlayout.h"
#include "window.h"
Window::Window( const QList< QImage > & images )
{
FlowLayout *flowLayout = new FlowLayout;
QList< QImage >::const_iterator it = images.begin();
QList< QImage >::const_iterator eIt = images.end();
while ( it != eIt )
{
const QImage & image = *it++;
QLabel * pLabel = new QLabel( this );
pLabel->setPixmap( QPixmap::fromImage( image ) );
flowLayout->addWidget( pLabel );
}
setLayout(flowLayout);
setWindowTitle(tr("Images in a Flow Layout"));
}
To copy to clipboard, switch view to plain text mode
Bookmarks