Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Custom QGraphicsPixmapItem

[solved] Custom QGraphicsPixmapItem

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 6.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ThaRez
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

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

      dmcr

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dmcr
        wrote on last edited by
        #3

        thanks ;)

        dmcr

        1 Reply Last reply
        0
        • T Offline
          T Offline
          ThaRez
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • T Offline
            T Offline
            ThaRez
            wrote on last edited by
            #5

            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 )

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dmcr
              wrote on last edited by
              #6

              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 ?

              dmcr

              1 Reply Last reply
              0
              • T Offline
                T Offline
                ThaRez
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dmcr
                  wrote on last edited by
                  #8

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

                  dmcr

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    ThaRez
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dmcr
                      wrote on last edited by
                      #10

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

                      dmcr

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved