Poka Yoke
Poka Yoke

Reputation: 403

How to attach a signal to qPixmap?

I'm creating an application using C++ Qt and I want to load multiple images. I would like to attach a signal to each image so that I can enable or disable them afterwards.

Any help?

Edit 1:

imageDlg = new QFileDialog();
imageList =  imageDlg->getOpenFileNames(this,
                         tr("Open Document"),
                         QDir::currentPath(),
                         tr("Image Files (*.png *.jpg);;All files (*.*)"));


QString imageName;
int x = -50;
int y = -50;
int n = 1;
double size = imageList.size();

if(imageList.isEmpty())
    return;

scene->clear();
setCursor(Qt::WaitCursor);


foreach(imageName,imageList)
{
    double val = (n/size)*100;
    ui->progressBar->setValue((int)val);


    image.load(imageName,"4",Qt::AutoColor);

    image = image.scaled(100,100,Qt::KeepAspectRatio,Qt::FastTransformation);
    imageNames.push_back(imageName.toStdString());

   // scene->setSceneRect(x,y,100,100);
    item = scene->addPixmap(image);
    item->setPos(x,y);
    x = x + 110;

    if(n%4 == 0)
    {
        x = -50;
        y = y + 90;
    }

    n++;
}

//ui->label_2->setText(strcat("10","image(s) loaded successfully"));
setCursor(Qt::ArrowCursor);
ui->imageGraphicsView->setScene(scene);

Upvotes: 0

Views: 873

Answers (1)

Mat
Mat

Reputation: 206689

You should be storing the QGraphicsPixmapItem* pointers you're getting back from:

scene->addPixmap();

(Use e.g. a QList<QGraphicsPixmapItem*> or another container of your choice.)

These are the objects that are displayed on your scene. You can change their appearance, show or hide them, change their opacity etc. through those pointers.

Look at the documentation for QGraphicsItem for detailed information about how you can manipulate these items.

QGraphicsItem doesn't inherit from QObject, it doesn't have signals or slots (the classes derived from it don't either). If you want to handle mouse events, you'll need to create a custom graphics item (derived from QGraphicsPixmapItem for example) and re-implement the event handling functions you're interested in.

Look at the Elastic Nodes example to get a sample of how you can handle mouse events for graphics items.

Upvotes: 1

Related Questions