[solved] Custom QGraphicsPixmapItem
-
Hello
I'm trying to create a QGraphicsPixmapItem that would allow me to switch the image via a signal/slot connect as well as to react to mouse clicks. I've come this far (see below) but can figure out what is wrong. First of all, the code doesn't seem to react to mouse clicks. I've not yet been able to test if the image switching works. Any help how to construct such an class is highly welcomed and appreciated. Thank you!
Best regardsRichard
@
#ifndef imgElement_H
#define imgElement_H#include <QGraphicsPixmapItem>
#include <QString>
#include <QPoint>#include "node.h"
#include "mainwindow.h"class imgElement : public QObject, public QGraphicsPixmapItem
{
public:
imgElement(const QPixmap &pixmap, QGraphicsItem *parent = 0,
QGraphicsScene *scene = 0);
~imgElement();private:
QPixmap mainImage;
QPoint mousePos;
QGraphicsScene *scene;protected:
virtual void mousePressEvent ( QMouseEvent * e );
virtual void mouseReleaseEvent ( QMouseEvent * e );private slots:
void switchImage(QString imgPath);};
#endif // imgElement_H
#include "imgElement.h"
imgElement::imgElement(const QPixmap &pixmap, QGraphicsItem *parent,
QGraphicsScene *scene)
: QGraphicsPixmapItem(pixmap, parent, scene){
this->scene = scene;
this->mainImage = pixmap;
}imgElement::~imgElement(){}
void imgElement::mousePressEvent ( QMouseEvent * e )
{
mousePos = e->pos();
}void imgElement::mouseReleaseEvent ( QMouseEvent * e )
{
// Do some interesting stuff
}void imgElement::switchImage(QString imgPath){
mainImage.load(imgPath);
this->setPixmap(mainImage);
}@*added to code tags by request (wasn't aware of them)
-
I tried the SIGNAL/SLOT but it failed as the "Q_OBJECT" was missing. The problem is, if I add it
@class Jhumar : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
Jhumar(const QPixmap &pixmap, QGraphicsItem *parent = 0,
QGraphicsScene *scene = 0); ...@I get errors saying "undefined reference to 'vtable for imgElement". How can I make it derrive from the object class, making the slot visible? This would probably solve the "event issue" as well... Thanks!
-
I got rid of the vtable error by rerunning qmake (http://sector.ynet.sk/qt4-tutorial/my-first-qt-gui-application.html) but the mouse evets still doesn't work (the image switch does however :D )
-
Hello. Don't know why, but when I remove the QObject inheritance the slot stops working... Yet, actuallyjust got it working, the trick to the mouse even was to use
@void mousePressEvent (QGraphicsSceneMouseEvent *event);@
instead of
@void imgElement::mousePressEvent ( QMouseEvent * e )@
Hopefully this will help someone avoid the same mistake. :)