I'm open to all opinions on the best way to do this, including "something else completely".
I'm trying to create objects to be displayed in QT5 with a base class containing many shared properties and virtual functions. These are graphics objects linked together by the user- I'd like them to function more or less like a linked list during evaluation, containing weak pointers to their neighbors.
QT wasn't able to resolve a std::variant containing pointers to the child objects, and I'm not sure if it's possible to do true polymorphism the way I envisioned it on QGraphicsObjects, since QVariant doesn't take user defined classes.
There's an excellent post here which is semi-related, but not quite how I'd like to handle UI/drawing, C++: How to get decoupled polymorphic behavior I want to keep the information in the QGraphicsObjects, then once the entry is all done, transfer to stl code that does the processing.
I'm also trying to make a distinction between clicks on the object and connector handles on the object, which I intended to use virtual mouseevent functions for.
The problem I have even with doing visitors or even dynamic_cast is with the required functions for QT, like boundingRect();
I've tried declaring it virtual, as below, and I'm still getting "undefined reference"
class BaseObject : public QGraphicsObject
{
Q_OBJECT
public:
//QT requirements
virtual QRectF boundingRect();
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
//Input tracking
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
public slots:
virtual void flip();
virtual void rotate();
virtual bool checkConstrained();
protected:
//Constructed only by inherited items
BaseObject(QPoint* base);
virtual ~BaseObject(){}
//Shared properties
QString name;
bool constrained;
//etc...
};
Here's one of the classes inheriting it:
class ChildObject : public BaseObject
{
Q_OBJECT
public:
ChildObject(QPoint* base, QPointer<BaseObject> linkInit1 = nullptr, QPointer<BaseObject> linkInit2 = nullptr, QPointer<BaseObject> linkInit3 = nullptr);
QRectF boundingRect() const override;
bool checkConstrained();
void flip();
void rotate();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
Is there a more sensible way to do this? So far I've also thought of letting the "connection handle" objects own pointers to other handles, but that just breaks OOP and you still end up needing code that asks "Which type of object am I owned by?", in addition to "which one of its handles am I?"