Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to override a function QGraphicsScene::items()



  • 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;
    }
    
    

  • Lifetime Qt Champion

    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.



  • 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:
    };
    

  • Qt Champions 2019

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



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



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


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    @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



  • 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:
    };



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


  • Lifetime Qt Champion

    What text ?



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


  • Lifetime Qt Champion

    You really are not clear.



  • @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.



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


Log in to reply