简体   繁体   中英

How to add an inherit QGraphicsEllipseItem to QGraphicsScene

I added multiple ellipses to my QGraphicsScene:

QGraphicsEllipseItem *ellipse;
ellipse = sceneCenter->addEllipse(xCor-4,yCor-4,imgW+7,imgH+7,blackpen,redBrush);

Now i want to add hoverEnterEvent so every time i drag my mouse to these ellipse it would do something, so i made this class:

#include <QGraphicsEllipseItem>
#include <QMessageBox>

class myEllipse : public QGraphicsEllipseItem
{
public:
    myEllipse(QGraphicsEllipseItem* parent);
    void hoverEnterEvent(int index);
};

#endif // MYELLIPSE_H

#include "myellipse.h"

myEllipse::myEllipse(QGraphicsEllipseItem* parent):QGraphicsEllipseItem(parent){
    setAcceptHoverEvents(true);
}


void myEllipse::hoverEnterEvent(int index){
    QMessageBox q;
    q.setText("hello");
    q.exec();
}

I want to replace QGraphicsEllipseItem by the object created by the class above. How do i do this?

At the moment, you're doing this: -

QGraphicsEllipseItem *ellipse;
ellipse = sceneCenter->addEllipse(xCor-4,yCor-4,imgW+7,imgH+7,blackpen,redBrush);

If you create an instance of your new class first, you can then add it afterwards and set its position and other properties:-

myEllipse * ellipse = new myEllipse(parent);
sceneCenter->addItem(ellipse);

ellipse->setRect(xCor-4,yCor-4,imgW+7,imgH+7);
ellipse->setPen(blackpen);
ellipse->setBrush(redBrush);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM