[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 regards

    Richard
    @
    #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)



  • Hello,
    It would be more readable if you put you code in the code tags....



  • thanks ;)



  • 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 )



  • I don't really know why you don't pass in the mousePress Event
    However, if your object inherits QGraphicsPixmapI, you don't have to makes it inherit with QObject, since QGraphicsPixmapI itself is a QObject... could you try without it ?



  • 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. :)



  • your constructor doesn't mention QGraphicsPixmapItem....--



  • Hello
    Correct me if I'm wrong, but shouldn't this part handle the initialization of the QGraphicsPixmapItem ?
    @: QGraphicsPixmapItem(pixmap, parent, scene)@



  • I think that if your element is a child of QGraphicsPixmapItem, your constructor must have an initialisation with some QGraphicsPixmapItem....


Log in to reply
 

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