I have to add multiples of two types of Graphics Items (QGraphicsRectItem
and QGraphicsEllipseItem
) to a QGraphicsScene
(which is added to QGraphicsView
). Each graphics item should be able to interact with the mouse and keyboard events. So initially I designed the classes as below.
class myQGraphicsRectItem : public QGraphicsRectItem
{
public:
explicit myQGraphicsRectItem();
private:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void focusOutEvent(QFocusEvent *event);
//Many events goes here
void fnCreateRect();
};
class myQGraphicsRectItem : public QGraphicsEllipseItem
{
public:
explicit myQGraphicsRectItem();
private:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void focusOutEvent(QFocusEvent *event);
//Many events goes here
void fnCreateCircle();
};
To know how to avoid duplicate event declaration and definition, I read the answers to:
and finally designed the class as below .
template <class T>
class myQGraphicsRectItem : public QGraphicsRectItem , public QGraphicsEllipseItem{
/*commented the below event methods in the base classes*/
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void focusOutEvent(QFocusEvent *event);
//Many events goes here
}
template <class T> myGraphicsItem<T>::myGraphicsItem()
{
this->T::setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsFocusable);
this->T::setFocus();
}
//**All event function definition goes here....**
template <class T> QGraphicsItem *myGraphicsItem<T>::fnGetRectObject()
{
fnCreateRect();
return (T*)this;
}
#include "mygraphicsitem.h"
#include "mygraphicsitem.cpp"
void MainWindow::on_PushButton_clicked()
{
myGraphicsItem<myQGraphicsRectItem> *rr = new myGraphicsItem<myQGraphicsRectItem>();
scene->addItem(rr->fnGetRectObject());
}
And the application is working as expected with this modified design. But I have some doubts with this design, as follows:
QGraphicsRectItem
and QGraphicsEllipseItem
inherit from QAbstractGraphicsShapeItem
. So do I need to handle the virtual base class concept?Wow, that's over thinking the solution to the problem!
any other best method is there (to write single code to handle events)?
For me, the question is not clear in what you're trying to achieve, possibly due to the attempted solution, so I'm going to assume that with the two classes, QGraphicsRectItem
and QGraphicsEllipseItem
, you're trying to have a single block of code to handle custom events, such as mouse, keys etc.
In this case, Qt provides you with all you need in the call to installSceneEventFilter.
Simply create a class that's derived from QGraphicsItem
and reimplement the events you want to handle. Then add this new class to the other instances with installSceneEventFilter
class QGraphicsItemEventsFilter : public QGraphicsItem
{
private:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void focusOutEvent(QFocusEvent *event);
};
Instantiate the filter and add it to the other classes
QGraphicsItemEventsFilter* pFilter = new QGraphicsItemEventsFilter;
QGraphicsRectItem* pRect = new QGraphicsRectItem;
pRect->installSceneEventFilter(pFilter);
QGraphicsEllipseItem* pEllipse = new QGraphicsEllipseItem;
pEllipse->installSceneEventFilter(pFilter);
Now all events for both pRect
and pEllipse
will be handled by the event filter, pFilter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With