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. How to override a function QGraphicsScene::items()
Forum Updated to NodeBB v4.3 + New Features

How to override a function QGraphicsScene::items()

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 1.2k Views 2 Watching
  • 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 Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      4
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        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
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #6

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

              mrjjM 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                2
                • M Mikeeeeee

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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
                  3
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on last edited by Mikeeeeee
                    #9

                    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
                    0
                    • M Offline
                      M Offline
                      Mikeeeeee
                      wrote on last edited by
                      #10

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

                      W 1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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
                        0
                        • M Offline
                          M Offline
                          Mikeeeeee
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            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
                            1
                            • M Mikeeeeee

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

                              W Offline
                              W Offline
                              wrosecrans
                              wrote on last edited by
                              #14

                              @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
                              2
                              • M Offline
                                M Offline
                                Mikeeeeee
                                wrote on last edited by
                                #15

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

                                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