How to display multiple images on MainWindow??



  • Hi everyone,

    i'm new here and stuck.

    In my first GUI, I have one push_button which contains certain functions and produces png images after each iteration in a loop. What I'm trying to do is to display those images in QLabel on my mainWindow using code mentioned below....
    @
    QPixmap myPic(QApplication::applicationDirPath()+"imageName" ); //image1.png, image2.png, image3.png
    QLabel *picLabel = new QLabel(ui->label_2);
    QPixmap scaledPic(myPic.scaled(ui->label_2->width(),ui->label_2->height()));
    picLabel->setPixmap( scaledPic );
    picLabel->show();
    @
    Above code is inside a for loop. The problem is it only displays last image (not one after another).
    I'm eagerly waiting for your reply.

    Thanks
    CogBot

    [Edit: Added @ tags around code -- mlong]



  • You will have to add the images to a layout, and then set the layout for <code>ui->label_2</code>. Otherwise the images are just displayed on top of each other.



  • Thanks for your reply. Could you please give me some sample code? I'm very new. Failed to manage.



  • Just pick the most suitable "layout":http://qt-project.org/doc/qt-4.8/layout.html, set it on the label and add the various images to it.

    [quote author="CognitiveRobot" date="1340602978"]
    @
    QHBoxLayout *layout = new QHBoxLayout; // or any other layout
    ui->label_2->setLayout(layout);
    ...
    QPixmap myPic(QApplication::applicationDirPath()+"imageName" );
    QLabel *picLabel = new QLabel;
    QPixmap scaledPic(myPic.scaled(ui->label_2->width(),ui->label_2->height()));
    picLabel->setPixmap( scaledPic );
    layout->addWidget(picLabel);
    @
    [/quote]



  • Lukas, thanks again. I couldn't figure it out using layout. But i managed another way. need to add picLabel->repaint() at the end of my mentioned code.



  • I tried to follow your code snippet for displaying all images from a folder. I read all images in QList<QImage> list and then use foreach loop to display.
    This code only displays one image. A blank area is displayed for more than one iterations..........
    @
    QHBoxLayout *layout = new QHBoxLayout; // or any other layout
    ui->label_2->setLayout(layout);
    ...
    QPixmap myPic(QApplication::applicationDirPath()+"imageName" );
    QLabel *picLabel = new QLabel;
    QPixmap scaledPic(myPic.scaled(ui->label_2->width(),ui->label_2->height()));
    picLabel->setPixmap( scaledPic );
    layout->addWidget(picLabel);
    @
    Any help is appreciated.



  • Did you put the whole code into your loop, or just the lines after <code>QPixmap myPic(...</code>?



  • Try this:
    @
    QVBoxLayout* PicsLayout=new QVBoxLayout(ui->label_2);
    ...
    //then inside the loop:
    QPixmap myPic(QApplication::applicationDirPath()+"imageName" ); //image1.png, image2.png,
    QLabel *picLabel = new QLabel(ui->label_2);
    picLabel->setScaledContents(true);
    picLabel->setPixmap(myPic);
    PicsLayout->addWidget(picLabel);
    @
    You should get all the images in a column.
    use QHBoxLayout instead of QVBoxLayout if you want them in a row or QGridLayout if you want them in a grid but, in this case, you should specify the number of column and row for each pic as additional arguments to addWidget



  • thanks VRonin for reply.

    @QList<QImage> images;
    foreach(QFileInfo fileInfo, filesList)
    {
    tempFileName += fileInfo.fileName();
    QImage image(tempFileName);
    copy = image.scaled(inputWidth.toUInt(),inputHeight.toUInt(),Qt::KeepAspectRatio);
    images.append(image);
    }
    //loop correctly populates the list and "images" list has 9 images

    QHBoxLayout *verticalPicsLayout=new QHBoxLayout(ui->imageLabel2);
    for(int i=0;i<images.size();i++)
    {
    QPixmap p(QPixmap::fromImage(images[i]));
    QLabel *picLabel = new QLabel(ui->imageLabel2);
    picLabel->setScaledContents(true);
    picLabel->setPixmap(p);
    verticalPicsLayout->addWidget(picLabel);
    }@

    It only shows the first image in list and not all the images....
    Am I missing something or doing something incorrectly?



  • Lukas, I posted the code in my recent post, please have a look at it. Any pointer for solving the problem.........
    [quote author="Lukas Geyer" date="1357645867"]Did you put the whole code into your loop, or just the lines after <code>QPixmap myPic(...</code>?[/quote]



  • Each image represents thumbnail of a video stream. Next step is to play the video stream by clicking on respective thumbnail.

    How will I implement click and play video after displaying the thumbnails from current implementation?

    Am I moving in right direction? if not, suggestions are welcome.



  • Did you lay out ui->imageLabel2?

    I wrote this small test code and the items are shown correctly:

    Test.h
    @
    #ifndef TESTQT_H
    #define TESTQT_H
    #include <QWidget>
    class QLabel;
    class TestQt : public QWidget
    {
    Q_OBJECT
    public:
    TestQt(QWidget parent = 0);
    private:
    QLabel
    MainLabel;
    };
    #endif
    @

    Test.cpp
    @
    #include "testqt.h"
    #include <QtGui>

    TestQt::TestQt(QWidget parent)
    : QWidget(parent)
    {
    setMinimumSize(100,100);
    MainLabel=new QLabel(this);
    QHBoxLayout
    MainLayout=new QHBoxLayout(this);
    MainLayout->addWidget(MainLabel);
    QHBoxLayout* HorFramesLayout=new QHBoxLayout(MainLabel);
    QList<QPixmap> PixList;
    for (int i=1;i<=9;i++){
    // Generate 9 20x20 pixmap filled with different colours
    PixList.append(QPixmap(20,20));
    PixList.last().fill(QColor(255i/9,125,125));
    }
    foreach(QPixmap pix, PixList){
    // put the pixmaps one next to the other
    QLabel
    PixLabel=new QLabel(MainLabel);
    PixLabel->setScaledContents(true);
    PixLabel->setPixmap(pix);
    HorFramesLayout->addWidget(PixLabel);
    }
    }
    @

    main.cpp
    @
    #include "testqt.h"
    #include <QtGui/QApplication>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    TestQt w;
    w.show();
    return a.exec();
    }
    @

    Hope this example helps



  • ok, I think i spotted the problem.
    @tempFileName += fileInfo.fileName();@
    should become
    @tempFileName = fileInfo.fileName();@

    You are just concatenating the filenames and the QImage constructor just uses the first one



  • Thanks for the correction, it works.

    [quote author="VRonin" date="1357663085"]ok, I think i spotted the problem.
    @tempFileName += fileInfo.fileName();@
    should become
    @tempFileName = fileInfo.fileName();@

    You are just concatenating the filenames and the QImage constructor just uses the first one[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.