Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [solved] Custom QGraphicsPixmapItem

    General and Desktop
    2
    10
    5831
    Loading More Posts
    • 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
      ThaRez last edited by

      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 Reply Quote 0
      • D
        dmcr last edited by

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

        dmcr

        1 Reply Last reply Reply Quote 0
        • D
          dmcr last edited by

          thanks ;)

          dmcr

          1 Reply Last reply Reply Quote 0
          • T
            ThaRez last edited by

            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 Reply Quote 0
            • T
              ThaRez last edited by

              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 Reply Quote 0
              • D
                dmcr last edited by

                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 Reply Quote 0
                • T
                  ThaRez last edited by

                  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 Reply Quote 0
                  • D
                    dmcr last edited by

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

                    dmcr

                    1 Reply Last reply Reply Quote 0
                    • T
                      ThaRez last edited by

                      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 Reply Quote 0
                      • D
                        dmcr last edited by

                        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 Reply Quote 0
                        • First post
                          Last post