Greetings.

I have subclassed QPushButton, and I'll call it MyPushButton here. Each MyPushButton widget also has a menu associated with it, and I'll call that class NodeMenu. MyPushButton widgets are organized in a custom window, and the user has the ability to split the window unlimited number of times, which in turn creates a new set of MyPushButton widgets of the same kind.

Instead of dynamically creating a new NodeMenu for each MyPushButton widget of the same kind, I figured I should rewrite the NodeMenu class as a singleton. This is what it looks like in short:

Qt Code:
  1. class NodeMenu : public QMenu{
  2.  
  3. public:
  4. typedef enum {MenuAnimation, MenuTransformers, MenuFractals, MenuColor} MenuType;
  5.  
  6. static NodeMenu *CreateMenu(MenuType menuType){
  7.  
  8. switch( menuType ){
  9. case MenuAnimation:
  10. static NodeMenu animationMenu(menuType);
  11. return &animationMenu;
  12.  
  13. case MenuTransformers:
  14. static NodeMenu transformersMenu(menuType);
  15. return &transformersMenu;
  16.  
  17. case MenuFractals:
  18. static NodeMenu fractalsMenu(menuType);
  19. return &fractalsMenu;
  20.  
  21. case MenuColor:
  22. static NodeMenu colorMenu(menuType);
  23. return &colorMenu;
  24. }
  25. }
  26.  
  27. private:
  28. NodeMenu(MenuType menuType);
  29. NodeMenu(const NodeMenu &);
  30. NodeMenu &operator=(const NodeMenu &);
  31. ~NodeMenu();
  32. };
To copy to clipboard, switch view to plain text mode 

Now, this works fine, except I can't pass a QWidget *parent to the NodeMenu because if the very first created window is destroyed, then parent becomes a dangling pointer and I get seg-fault.

The NodeMenu doesn't seem to need a parent here, and the singleton gets destroyed during exit. But I would like to ask if anyone thinks there is anything wrong with this approach? I do save little bit of memory this way, but could this approach cause problems later on?