class Image : public QGraphicsItem{public: Image(const QString &fileName); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);protected: void mousePressEvent(QGraphicsSceneMouseEvent *event);private: QImage image;};
Image::Image(const QString &fileName){ image = QImage(fileName);}void Image::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/){ painter->drawImage(0, 0, image); painter->setRenderHint(QPainter::Antialiasing, true);}void Image::mousePressEvent(QGraphicsSceneMouseEvent *event){ //do something QGraphicsItem::mousePressEvent(event);}
class Image : public QGraphicsItem{public: Image(const QString &fileName); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); const QMovie *getMovie();protected: void mousePressEvent(QGraphicsSceneMouseEvent *event);private: QMovie *movie;};
Image::Image(const QString &fileName){ movie = new QMovie(fileName); movie->start();}void Image::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/){ painter->drawPixmap(0, 0, movie->currentPixmap()); painter->setRenderHint(QPainter::Antialiasing, true);}void Image::mousePressEvent(QGraphicsSceneMouseEvent *event){ //do something QGraphicsItem::mousePressEvent(event);}
Image *img = new Image(fileName);connect(img->getMovie(), SIGNAL(frameChanged(int)), scene, SLOT(update()));
connect(img->getMovie(), SIGNAL(frameChanged(int)), scene, SLOT(update()));
class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); ...private: QSignalMapper *signalMapper; QList<Image *> imageList; ...private slots: void addImage(const QString &fileName); void updateItem(int i); ...}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ ... signalMapper = new QSignalMapper(this); ...}void MainWindow::addImage(const QString &fileName){ Image *img = new Image(fileName); imageList.append(img); connect(img->getMovie(), SIGNAL(frameChanged(int)), signalMapper, SLOT(map())); signalMapper->setMapping(img->getMovie(), imageList.indexOf(img)); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(updateItem(int)));}void MainWindow::updateImage(int i){ imageList.at(i)->update();}
worker->setPixmap(pixmap); worker->update();