tags:

views:

137

answers:

1

I have a QHBox with a custom widget that inherits QLabel which iterates through all image files in a dir and generates a thumbnail.

This part is working fine, however I need to implement the functionality to display the original image from which the thumbnail was generated in a central QLabel widget for diplaying pictures.

What would be the best way of doing this? Right now the clicked() signal is only displaying the QPixmap from the QLabel(thumbnail), what I need it to do is load the original full size image on a QLabel that is at the center.

I'm guessing I will need a list of some sort to identify each thumbnail with its unique full size image, but im not sure how to implement such thing.

Please advise me on should I go about implementing this as I feel i'm a bit lost on what to do.

My code right now looks like this:

QList<ImageLabel *> labels;
int imagenum = 0;
foreach(const QString &path, files) {
   QPixmap px(path);
   labels.append(new ImageLabel);
   labels[imagenum]->setPixmap(px.scaledToHeight(90));
   qhbox->addWidget(labels[imagenum]);
   connect(labels[imagenum], SIGNAL(clicked(const QPixmap&)), this, SLOT(setImage(const QPixmap &)));
   imagenum++;
}

Thank you in advance for your help.

+2  A: 

Generally speaking, it's a bad idea to emit complex objects through signal and slots. If you need to emit complex objects, it's better to store them in a QMap and emit the key of the QMap (a string or an integer).

For your problem, I think you need to take the following approach:

  • store the pixmap path in your ImageLabel, so that getting to know which ImageLabel was clicked is enough information to display the full picture
  • use a QSignalMapper, the example in the documentation is extremely close to what you want to do.

Basically, you can arrange the signal mapper to emit a signal with a property that is part of the widget emitting the signal. In your case, you could map each ImageLabel with the path if the image it is displaying, and have the signal mapper emit the path as part of the signal.

Some untested code attempt:

QList<ImageLabel *> labels;
signalMapper = new QSignalMapper( this );
foreach(const QString &path, files) {
    QPixmap px(path);
    imgLabel = new ImageLabel();
    labels.append( imgLabel );
    imgLabel->setPixmap(px.scaledToHeight(90));
    qhbox->addWidget(imgLabel);
    connect(imgLabel, SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping( imgLabel, path );
}
connect( signalMapper, SIGNAL(mapped(const QString &)), 
         this, SLOT(setImagePath()));

I removed the use of imagenum, it was not useful for that code.

Bluebird75