Hi,

I'm currently trying to design a program that allows me to add and remove QWidgets(e.g. QPushButtons and QLineEdits) to a QMainWindow at runtime. So far this works pretty well, but now i recognize that my class design isn't that great and results in redundant code. I was hoping that some of you guys might have a better idea.

My QWidgets that I will add to the QMainWindow(more specifically a custom QScreenLayout) all need some special functions for serialization and special mouse events. This is what I've got so far:

Qt Code:
  1. class ScreenPushButton : public QPushButton, public DisplayElement
  2. {
  3. Q_OBJECT
  4. public:
  5. ScreenPushButton(QWidget *parent = 0);
  6. ~ScreenPushButton();
  7.  
  8. virtual void serialize(QDataStream &outstream) override;
  9. virtual void deserialize(QDataStream &instream) override;
  10.  
  11. private:
  12. QPoint offset_;
  13. virtual void mousePressEvent(QMouseEvent *event) override;
  14. virtual void mouseMoveEvent(QMouseEvent *event) override;
  15. };
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. class DisplayElement
  2. {
  3. public:
  4. virtual void serialize(QDataStream &outstream) = 0;
  5. virtual void deserialize(QDataStream &instream) = 0;
  6.  
  7. quint8 dataType() {return dataType_;}
  8.  
  9. protected:
  10. quint8 dataType_;
  11. };
To copy to clipboard, switch view to plain text mode 

Now, any other custom widget like for example ScreeLineEdit look quite the same as ScreenPushButton. Serialize() and deserialize() will always differ, but the following part could(and should) be shared among all custom widgets.
Qt Code:
  1. private:
  2. QPoint offset_;
  3. virtual void mousePressEvent(QMouseEvent *event) override;
  4. virtual void mouseMoveEvent(QMouseEvent *event) override;
To copy to clipboard, switch view to plain text mode 

My first very naive try was to move the definition of the mouse events to DisplayElement but that of course won't work because then these function signatures would be ambiguous. Calling this function could either be a call to QPushButton::mousePressEvent() or a call to DisplayItem::mousePressEvent().

Another problem I stumbled upon is that only the first inherited class can be a Q_OBJECT.

Any ideas how to resolve this issue? Do I need to provide more information?

Kind regards
k