How to implement signal and slots in QGraphicsRectItem? subclassed qobject and qgraphicsrectitem but getting errors










0















I am creating object from QGraphicsRectItem and adding to the Qgraphicscene(scene).
I want to get every movement(pos) of object(qgraphicsrectitem) so that I subclassed Qgraphicsrectitem. But in this class I am getting some errors.
How can I get object position changed in scene ?



error:




'staticMetaObject' is not a member of 'QGraphicsRectItem'
{ &QGraphicsRectItem::staticMetaObject, qt_meta_stringdata_ItemHandler.data




ItemHandler.h



#include <QObject>
#include <QGraphicsRectItem>

class ItemHandler : public QGraphicsRectItem, public QObject

Q_OBJECT

public:
ItemHandler(QGraphicsItem *parent = 0 );
~ItemHandler();

protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);

signals:
void objectHandlePosChanged(QPointF value);
;


ItemHandler.cpp



 #include "itemhandler.h"

ItemHandler::ItemHandler(QGraphicsItem *parent) : QGraphicsRectItem(parent)

setFlag(QGraphicsItem::ItemSendsGeometryChanges);


ItemHandler::~ItemHandler()




QVariant ItemHandler::itemChange(QGraphicsItem::GraphicsItemChange change,
const QVariant &value)

QPointF newPos = value.toPointF();
emit objectHandlePosChanged(newPos);










share|improve this question




























    0















    I am creating object from QGraphicsRectItem and adding to the Qgraphicscene(scene).
    I want to get every movement(pos) of object(qgraphicsrectitem) so that I subclassed Qgraphicsrectitem. But in this class I am getting some errors.
    How can I get object position changed in scene ?



    error:




    'staticMetaObject' is not a member of 'QGraphicsRectItem'
    { &QGraphicsRectItem::staticMetaObject, qt_meta_stringdata_ItemHandler.data




    ItemHandler.h



    #include <QObject>
    #include <QGraphicsRectItem>

    class ItemHandler : public QGraphicsRectItem, public QObject

    Q_OBJECT

    public:
    ItemHandler(QGraphicsItem *parent = 0 );
    ~ItemHandler();

    protected:
    QVariant itemChange(GraphicsItemChange change, const QVariant &value);

    signals:
    void objectHandlePosChanged(QPointF value);
    ;


    ItemHandler.cpp



     #include "itemhandler.h"

    ItemHandler::ItemHandler(QGraphicsItem *parent) : QGraphicsRectItem(parent)

    setFlag(QGraphicsItem::ItemSendsGeometryChanges);


    ItemHandler::~ItemHandler()




    QVariant ItemHandler::itemChange(QGraphicsItem::GraphicsItemChange change,
    const QVariant &value)

    QPointF newPos = value.toPointF();
    emit objectHandlePosChanged(newPos);










    share|improve this question


























      0












      0








      0








      I am creating object from QGraphicsRectItem and adding to the Qgraphicscene(scene).
      I want to get every movement(pos) of object(qgraphicsrectitem) so that I subclassed Qgraphicsrectitem. But in this class I am getting some errors.
      How can I get object position changed in scene ?



      error:




      'staticMetaObject' is not a member of 'QGraphicsRectItem'
      { &QGraphicsRectItem::staticMetaObject, qt_meta_stringdata_ItemHandler.data




      ItemHandler.h



      #include <QObject>
      #include <QGraphicsRectItem>

      class ItemHandler : public QGraphicsRectItem, public QObject

      Q_OBJECT

      public:
      ItemHandler(QGraphicsItem *parent = 0 );
      ~ItemHandler();

      protected:
      QVariant itemChange(GraphicsItemChange change, const QVariant &value);

      signals:
      void objectHandlePosChanged(QPointF value);
      ;


      ItemHandler.cpp



       #include "itemhandler.h"

      ItemHandler::ItemHandler(QGraphicsItem *parent) : QGraphicsRectItem(parent)

      setFlag(QGraphicsItem::ItemSendsGeometryChanges);


      ItemHandler::~ItemHandler()




      QVariant ItemHandler::itemChange(QGraphicsItem::GraphicsItemChange change,
      const QVariant &value)

      QPointF newPos = value.toPointF();
      emit objectHandlePosChanged(newPos);










      share|improve this question
















      I am creating object from QGraphicsRectItem and adding to the Qgraphicscene(scene).
      I want to get every movement(pos) of object(qgraphicsrectitem) so that I subclassed Qgraphicsrectitem. But in this class I am getting some errors.
      How can I get object position changed in scene ?



      error:




      'staticMetaObject' is not a member of 'QGraphicsRectItem'
      { &QGraphicsRectItem::staticMetaObject, qt_meta_stringdata_ItemHandler.data




      ItemHandler.h



      #include <QObject>
      #include <QGraphicsRectItem>

      class ItemHandler : public QGraphicsRectItem, public QObject

      Q_OBJECT

      public:
      ItemHandler(QGraphicsItem *parent = 0 );
      ~ItemHandler();

      protected:
      QVariant itemChange(GraphicsItemChange change, const QVariant &value);

      signals:
      void objectHandlePosChanged(QPointF value);
      ;


      ItemHandler.cpp



       #include "itemhandler.h"

      ItemHandler::ItemHandler(QGraphicsItem *parent) : QGraphicsRectItem(parent)

      setFlag(QGraphicsItem::ItemSendsGeometryChanges);


      ItemHandler::~ItemHandler()




      QVariant ItemHandler::itemChange(QGraphicsItem::GraphicsItemChange change,
      const QVariant &value)

      QPointF newPos = value.toPointF();
      emit objectHandlePosChanged(newPos);







      c++ qt signals-slots qgraphicsscene qgraphicsitem






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 22:43









      AAEM

      770318




      770318










      asked Nov 13 '18 at 7:14









      SagarSagar

      13610




      13610






















          1 Answer
          1






          active

          oldest

          votes


















          1














          In you header file, inherit first from QObject, as follows:



          #include <QObject>
          #include <QGraphicsRectItem>

          class ItemHandler : public QObject, public QGraphicsRectItem

          Q_OBJECT

          public:
          ItemHandler(QGraphicsItem *parent = 0 );
          ~ItemHandler();

          protected:
          QVariant itemChange(GraphicsItemChange change, const QVariant &value);

          signals:
          void objectHandlePosChanged(QPointF value);
          ;


          And this is my test main function (sorry it's just a sandbox):



          int main(int argc, char *argv)



          QApplication app(argc, argv);

          MainWindow* mywindow = new MainWindow();

          QGraphicsScene scene;
          ItemHandler *item = new ItemHandler;
          item->setRect(10.0, 10.0, 10.0, 10.0);

          scene.addItem(item);

          QApplication::connect(item, SIGNAL(objectHandlePosChanged(QPointF)), mywindow, SLOT(moved(QPointF)));

          QGraphicsView view(&scene);
          view.setFixedSize(250, 250);
          view.setWindowTitle("QGraphicsItem Test");
          item->setPos(-100, -100);
          item->setPos(-200, -200);
          view.show();
          return app.exec();




          I just used an empty MainWindow that is never displayed to have a slot to connect to the signal, there's no point in having a MainWindow: it was already there and I modified it instead of creating a new class. The example works, fires the signal and displays the rectangle.






          share|improve this answer

























          • Thanks for quick reply..I tried this after adding to scene its not showing rectangle. By debugging get to know constructor giving qObject ptr not qgraphicsrectitem pointer

            – Sagar
            Nov 13 '18 at 8:58






          • 1





            I tried it, it works and fires the objectHandlePosChanged signal. I can't help more unless you post a complete working example etc. etc.

            – L.C.
            Nov 13 '18 at 9:33











          • can u post... creating instance and connect in your example...2 line code

            – Sagar
            Nov 13 '18 at 10:06












          • I added a working example of how to create and connect your ItemHandler, hope it helps. Next time please put a minimal working example to reproduce the problem.

            – L.C.
            Nov 13 '18 at 13:02










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275699%2fhow-to-implement-signal-and-slots-in-qgraphicsrectitem-subclassed-qobject-and-q%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In you header file, inherit first from QObject, as follows:



          #include <QObject>
          #include <QGraphicsRectItem>

          class ItemHandler : public QObject, public QGraphicsRectItem

          Q_OBJECT

          public:
          ItemHandler(QGraphicsItem *parent = 0 );
          ~ItemHandler();

          protected:
          QVariant itemChange(GraphicsItemChange change, const QVariant &value);

          signals:
          void objectHandlePosChanged(QPointF value);
          ;


          And this is my test main function (sorry it's just a sandbox):



          int main(int argc, char *argv)



          QApplication app(argc, argv);

          MainWindow* mywindow = new MainWindow();

          QGraphicsScene scene;
          ItemHandler *item = new ItemHandler;
          item->setRect(10.0, 10.0, 10.0, 10.0);

          scene.addItem(item);

          QApplication::connect(item, SIGNAL(objectHandlePosChanged(QPointF)), mywindow, SLOT(moved(QPointF)));

          QGraphicsView view(&scene);
          view.setFixedSize(250, 250);
          view.setWindowTitle("QGraphicsItem Test");
          item->setPos(-100, -100);
          item->setPos(-200, -200);
          view.show();
          return app.exec();




          I just used an empty MainWindow that is never displayed to have a slot to connect to the signal, there's no point in having a MainWindow: it was already there and I modified it instead of creating a new class. The example works, fires the signal and displays the rectangle.






          share|improve this answer

























          • Thanks for quick reply..I tried this after adding to scene its not showing rectangle. By debugging get to know constructor giving qObject ptr not qgraphicsrectitem pointer

            – Sagar
            Nov 13 '18 at 8:58






          • 1





            I tried it, it works and fires the objectHandlePosChanged signal. I can't help more unless you post a complete working example etc. etc.

            – L.C.
            Nov 13 '18 at 9:33











          • can u post... creating instance and connect in your example...2 line code

            – Sagar
            Nov 13 '18 at 10:06












          • I added a working example of how to create and connect your ItemHandler, hope it helps. Next time please put a minimal working example to reproduce the problem.

            – L.C.
            Nov 13 '18 at 13:02















          1














          In you header file, inherit first from QObject, as follows:



          #include <QObject>
          #include <QGraphicsRectItem>

          class ItemHandler : public QObject, public QGraphicsRectItem

          Q_OBJECT

          public:
          ItemHandler(QGraphicsItem *parent = 0 );
          ~ItemHandler();

          protected:
          QVariant itemChange(GraphicsItemChange change, const QVariant &value);

          signals:
          void objectHandlePosChanged(QPointF value);
          ;


          And this is my test main function (sorry it's just a sandbox):



          int main(int argc, char *argv)



          QApplication app(argc, argv);

          MainWindow* mywindow = new MainWindow();

          QGraphicsScene scene;
          ItemHandler *item = new ItemHandler;
          item->setRect(10.0, 10.0, 10.0, 10.0);

          scene.addItem(item);

          QApplication::connect(item, SIGNAL(objectHandlePosChanged(QPointF)), mywindow, SLOT(moved(QPointF)));

          QGraphicsView view(&scene);
          view.setFixedSize(250, 250);
          view.setWindowTitle("QGraphicsItem Test");
          item->setPos(-100, -100);
          item->setPos(-200, -200);
          view.show();
          return app.exec();




          I just used an empty MainWindow that is never displayed to have a slot to connect to the signal, there's no point in having a MainWindow: it was already there and I modified it instead of creating a new class. The example works, fires the signal and displays the rectangle.






          share|improve this answer

























          • Thanks for quick reply..I tried this after adding to scene its not showing rectangle. By debugging get to know constructor giving qObject ptr not qgraphicsrectitem pointer

            – Sagar
            Nov 13 '18 at 8:58






          • 1





            I tried it, it works and fires the objectHandlePosChanged signal. I can't help more unless you post a complete working example etc. etc.

            – L.C.
            Nov 13 '18 at 9:33











          • can u post... creating instance and connect in your example...2 line code

            – Sagar
            Nov 13 '18 at 10:06












          • I added a working example of how to create and connect your ItemHandler, hope it helps. Next time please put a minimal working example to reproduce the problem.

            – L.C.
            Nov 13 '18 at 13:02













          1












          1








          1







          In you header file, inherit first from QObject, as follows:



          #include <QObject>
          #include <QGraphicsRectItem>

          class ItemHandler : public QObject, public QGraphicsRectItem

          Q_OBJECT

          public:
          ItemHandler(QGraphicsItem *parent = 0 );
          ~ItemHandler();

          protected:
          QVariant itemChange(GraphicsItemChange change, const QVariant &value);

          signals:
          void objectHandlePosChanged(QPointF value);
          ;


          And this is my test main function (sorry it's just a sandbox):



          int main(int argc, char *argv)



          QApplication app(argc, argv);

          MainWindow* mywindow = new MainWindow();

          QGraphicsScene scene;
          ItemHandler *item = new ItemHandler;
          item->setRect(10.0, 10.0, 10.0, 10.0);

          scene.addItem(item);

          QApplication::connect(item, SIGNAL(objectHandlePosChanged(QPointF)), mywindow, SLOT(moved(QPointF)));

          QGraphicsView view(&scene);
          view.setFixedSize(250, 250);
          view.setWindowTitle("QGraphicsItem Test");
          item->setPos(-100, -100);
          item->setPos(-200, -200);
          view.show();
          return app.exec();




          I just used an empty MainWindow that is never displayed to have a slot to connect to the signal, there's no point in having a MainWindow: it was already there and I modified it instead of creating a new class. The example works, fires the signal and displays the rectangle.






          share|improve this answer















          In you header file, inherit first from QObject, as follows:



          #include <QObject>
          #include <QGraphicsRectItem>

          class ItemHandler : public QObject, public QGraphicsRectItem

          Q_OBJECT

          public:
          ItemHandler(QGraphicsItem *parent = 0 );
          ~ItemHandler();

          protected:
          QVariant itemChange(GraphicsItemChange change, const QVariant &value);

          signals:
          void objectHandlePosChanged(QPointF value);
          ;


          And this is my test main function (sorry it's just a sandbox):



          int main(int argc, char *argv)



          QApplication app(argc, argv);

          MainWindow* mywindow = new MainWindow();

          QGraphicsScene scene;
          ItemHandler *item = new ItemHandler;
          item->setRect(10.0, 10.0, 10.0, 10.0);

          scene.addItem(item);

          QApplication::connect(item, SIGNAL(objectHandlePosChanged(QPointF)), mywindow, SLOT(moved(QPointF)));

          QGraphicsView view(&scene);
          view.setFixedSize(250, 250);
          view.setWindowTitle("QGraphicsItem Test");
          item->setPos(-100, -100);
          item->setPos(-200, -200);
          view.show();
          return app.exec();




          I just used an empty MainWindow that is never displayed to have a slot to connect to the signal, there's no point in having a MainWindow: it was already there and I modified it instead of creating a new class. The example works, fires the signal and displays the rectangle.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 12:34

























          answered Nov 13 '18 at 8:38









          L.C.L.C.

          347212




          347212












          • Thanks for quick reply..I tried this after adding to scene its not showing rectangle. By debugging get to know constructor giving qObject ptr not qgraphicsrectitem pointer

            – Sagar
            Nov 13 '18 at 8:58






          • 1





            I tried it, it works and fires the objectHandlePosChanged signal. I can't help more unless you post a complete working example etc. etc.

            – L.C.
            Nov 13 '18 at 9:33











          • can u post... creating instance and connect in your example...2 line code

            – Sagar
            Nov 13 '18 at 10:06












          • I added a working example of how to create and connect your ItemHandler, hope it helps. Next time please put a minimal working example to reproduce the problem.

            – L.C.
            Nov 13 '18 at 13:02

















          • Thanks for quick reply..I tried this after adding to scene its not showing rectangle. By debugging get to know constructor giving qObject ptr not qgraphicsrectitem pointer

            – Sagar
            Nov 13 '18 at 8:58






          • 1





            I tried it, it works and fires the objectHandlePosChanged signal. I can't help more unless you post a complete working example etc. etc.

            – L.C.
            Nov 13 '18 at 9:33











          • can u post... creating instance and connect in your example...2 line code

            – Sagar
            Nov 13 '18 at 10:06












          • I added a working example of how to create and connect your ItemHandler, hope it helps. Next time please put a minimal working example to reproduce the problem.

            – L.C.
            Nov 13 '18 at 13:02
















          Thanks for quick reply..I tried this after adding to scene its not showing rectangle. By debugging get to know constructor giving qObject ptr not qgraphicsrectitem pointer

          – Sagar
          Nov 13 '18 at 8:58





          Thanks for quick reply..I tried this after adding to scene its not showing rectangle. By debugging get to know constructor giving qObject ptr not qgraphicsrectitem pointer

          – Sagar
          Nov 13 '18 at 8:58




          1




          1





          I tried it, it works and fires the objectHandlePosChanged signal. I can't help more unless you post a complete working example etc. etc.

          – L.C.
          Nov 13 '18 at 9:33





          I tried it, it works and fires the objectHandlePosChanged signal. I can't help more unless you post a complete working example etc. etc.

          – L.C.
          Nov 13 '18 at 9:33













          can u post... creating instance and connect in your example...2 line code

          – Sagar
          Nov 13 '18 at 10:06






          can u post... creating instance and connect in your example...2 line code

          – Sagar
          Nov 13 '18 at 10:06














          I added a working example of how to create and connect your ItemHandler, hope it helps. Next time please put a minimal working example to reproduce the problem.

          – L.C.
          Nov 13 '18 at 13:02





          I added a working example of how to create and connect your ItemHandler, hope it helps. Next time please put a minimal working example to reproduce the problem.

          – L.C.
          Nov 13 '18 at 13:02

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275699%2fhow-to-implement-signal-and-slots-in-qgraphicsrectitem-subclassed-qobject-and-q%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo