1

My application can launch multiple instances of QMainWindow, and I would like each instance to have the same menu. If there are no instances initialized but the application is still running (possible on Mac OS X), I would still like the same menu to be displayed but with a few items disabled.

How would I go about doing this? Would it work to subclass QMenu or QMenuBar, turn the subclass into a singleton, and pass that to each QMainWindow?

Pino
  • 7,468
  • 6
  • 50
  • 69
Joey Kleingers
  • 297
  • 1
  • 4
  • 13
  • Don't you want an MDI application? – Droppy Jun 08 '15 at 14:55
  • Yes, but I also want to be able to use the same menu items that are in my `QMainWindow` menu outside of the `QMainWindow`. For example, if I have a "New..." menu item that creates a new instance of `QMainWindow` when pressed, I want to be able to create new instances both from the `QMainWindow` menu and from the global Mac OS X menu when a `QMainWindow` is not instantiated yet but the application is still running. – Joey Kleingers Jun 08 '15 at 16:36
  • I would assume that this would require having a `QMenu` subclass with a singleton pattern. I am trying to avoid having two separate menus (the `QMainWindow` menu and a menu that the application defaults to when there is no active `QMainWindow`). Two separate menus would require having duplicate code if items in both menus have the exact same functionality. – Joey Kleingers Jun 08 '15 at 16:39

1 Answers1

2

QMenu follows Qt's concept of ownage, and each menu or action can be owned only by one parent. There are ways to overcome that, but that wouldn't be the right thing to do. And subclassing can't change much in this case, especially not the ownage model.

I advise you to create a menu factory that creates separate menu and actions for each window (and one for no windows case) and connects each action to a corresponding slot (either in the window class or in some global class for global actions).

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127