Qt Forum

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

    Forum Updated on Feb 6th

    Solved How to override a function QGraphicsScene::items()

    General and Desktop
    5
    15
    323
    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.
    • M
      Mikeeeeee last edited by

      Hi!
      How to override a function QGraphicsScene::items()
      I made a blank, but I do not understand what to do next

      #include "QGraphicsScene"
      #include "QObject"
      #include "moveitem.h"
      #include "QVector"
      
      
      class MyQGraphicsScene : public QObject, public QGraphicsScene
      {
          Q_OBJECT
      
      public:
          MyQGraphicsScene();
          ~MyQGraphicsScene(){;}
          
      public slots:
          QVector <MoveItem> myItems();
      
      };
      
      #include "myqgraphicsscene.h"
      
      MyQGraphicsScene::MyQGraphicsScene()
      {
          
      }
      
      QVector<MoveItem> MyQGraphicsScene::myItems()
      {
          QVector<MoveItem> myVector;
          
          return  myVector;
      }
      
      
      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        Its not virtual so you cant override it.

        And why would you?

        What is the reason you cant use the normal items() ?

        I think you are trying to solve a problem the wrong way so, please
        give details about the requirement that made you feel you needed your own
        item() function.

        1 Reply Last reply Reply Quote 4
        • M
          Mikeeeeee last edited by

          I created such a class and would like to refer to its ellements:

          class MoveItem : public QObject, public QGraphicsItem
          {
              Q_OBJECT
          public:
              explicit MoveItem(QObject *parent = 0);
              ~MoveItem();
              int id;
              int width, height;
              QImage image;
          
              int getId() const;
              void setImageDark(int id);
          
          signals:
          
          private:
              QRectF boundingRect() const;
              void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
              void mouseMoveEvent(QGraphicsSceneMouseEvent *event);     //движение с нажатой кнопкой
              void mousePressEvent(QGraphicsSceneMouseEvent *event);    //нажатие кнопки
              void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);  //отпускание кнопки
          
          
          public slots:
          };
          
          1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

            And why can't you simply call QGraphicsScene::items() in there?

            Qt has to stay free or it will die.

            1 Reply Last reply Reply Quote 1
            • M
              Mikeeeeee last edited by

              I can, but I can't access my objects and use the function setImageDark(int id)

              1 Reply Last reply Reply Quote 0
              • M
                Mikeeeeee last edited by

                Maybe you know how to get the text of the original function QGraphicsScene::items() ?

                mrjj 1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  Hi,

                  Might be silly question but since you are not using any feature of QObject in your MoveItem why make it a QObject ?

                  If you really need that, there's already an existing class called QGraphicsObject.

                  Next, if your scene is only composed of your MoveItem, then you can go through the list and cast each pointer to the correct class and do whatever you need with it.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply Reply Quote 2
                  • mrjj
                    mrjj Lifetime Qt Champion @Mikeeeeee last edited by mrjj

                    @mikeeeeee
                    Hi
                    When using items(),
                    you just have to cast to your own type to use any function you defined.
                    (if cast dont fail, it IS your type)

                    https://doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast

                    and please note it says

                    Reimplement the type() function

                    1 Reply Last reply Reply Quote 3
                    • M
                      Mikeeeeee last edited by Mikeeeeee

                      As items() to cause my type?
                      I write such objects of the MoveItem class :
                      ```
                      for (int i = 0; i< id1.size(); i++)
                      {
                      MoveItem *item = new MoveItem();
                      item->setPos(x1[i], y1[i]);
                      item->id = id1[i]; // set id
                      item->width = width1[i];
                      item->height = height1[i];
                      scene->addItem(item);
                      }

                      Class MoveItem :
                      

                      class MoveItem : public QObject, public QGraphicsItem
                      {
                      Q_OBJECT
                      public:
                      explicit MoveItem(QObject *parent = 0);
                      ~MoveItem();
                      int id;
                      int width, height;
                      QImage image;

                      int getId() const;
                      void setImageDark(int id);
                      

                      signals:

                      private:
                      QRectF boundingRect() const;
                      void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
                      void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
                      void mousePressEvent(QGraphicsSceneMouseEvent *event);
                      void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

                      public slots:
                      };

                      1 Reply Last reply Reply Quote 0
                      • M
                        Mikeeeeee last edited by

                        Maybe you know how to get the text of the original function QGraphicsScene::items() ?

                        W 1 Reply Last reply Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          What text ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply Reply Quote 0
                          • M
                            Mikeeeeee last edited by

                            The function code itself. Apparently it can take a few alterations.

                            1 Reply Last reply Reply Quote 0
                            • SGaist
                              SGaist Lifetime Qt Champion last edited by

                              You really are not clear.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply Reply Quote 1
                              • W
                                wrosecrans @Mikeeeeee last edited by

                                @mikeeeeee You don't need to modify items() or QGraphicsScene to do what you want. If you add your custom items to the scene, they will be in the list of items returned. Just have some other function where you iterate through the pointers to items to do what you want with the ones that are the type you are looking for. It's a very normal use case, and modifying QGraphicsScene to have an unusual behavior for items() is only going to make your life difficult.

                                1 Reply Last reply Reply Quote 2
                                • M
                                  Mikeeeeee last edited by

                                  Added elements in vector and so was able to to them turn.

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post