Results 1 to 4 of 4

Thread: Singleton Menu!

  1. #1
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Singleton Menu!

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Singleton Menu!

    I figured I should rewrite the NodeMenu class as a singleton.
    How come?

    If you need multiple instances of your button, then Singleton is exactly what you DON'T need.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Re: Singleton Menu!

    Quote Originally Posted by high_flyer View Post
    How come?

    If you need multiple instances of your button, then Singleton is exactly what you DON'T need.
    I do need multiple instances of the button, but not the menu. Only one menu is shown at any given time.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Singleton Menu!

    Sorry, I wrongly understood that you made your button a singleton.
    How about using QAction instead of QMenu as base class?
    I think that conceptually it is more correct.
    Also, QAction is not a gui element, which makes it independent of any existing GUI elements, thus, instead giving it widgets, you give it to widgets.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Docked Widget Menu
    By Chuk in forum Qt Programming
    Replies: 6
    Last Post: 3rd July 2013, 14:12
  2. Additional keyboard shortcut for a menu
    By negritot in forum Qt Programming
    Replies: 5
    Last Post: 17th September 2009, 05:58
  3. Replies: 0
    Last Post: 29th July 2009, 19:27
  4. MainWindow Menu bug in Qt 4.2.3?
    By No-Nonsense in forum Qt Programming
    Replies: 4
    Last Post: 11th March 2007, 11:47
  5. Tracking separators in a menu (insertSeparator)
    By PrimeCP in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2006, 18:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.