Solved How to override a function QGraphicsScene::items()
-
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() ?
-
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.
-
@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() ?
-
What text ?
-
The function code itself. Apparently it can take a few alterations.
-
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.