[PyQt] question about dip.ui MenuBar

Phil Thompson phil at riverbankcomputing.com
Fri Sep 10 13:58:12 BST 2010


On Fri, 10 Sep 2010 08:21:48 -0400, Darren Dale <dsdale24 at gmail.com>
wrote:
> I have a question about the menubar example at
>
http://www.riverbankcomputing.co.uk/static/Docs/dip/ui_tutorial.html#menus
> . The example includes the following:
> 
> -------------
> # We need a toolkit to create the widgets.
> toolkit = QtToolkit()
> 
> # Create the main window.
> main_window = QMainWindow()
> 
> # Create the action.
> exit_action = QAction("&Exit", None, triggered=app.quit)
> 
> # Define the action hierarchy.
> action_collection = ActionCollection(title="&File",
members=[exit_action])
> 
> # Create the factory that will create instances of the action hierarchy.
> menu_bar = MenuBar(action_collection)
> 
> # Create an instance of the action hierarchy using QMenuBar and QMenu.
> qmenubar = menu_bar(toolkit)
> ------------
> 
> Based on its function, it looks like MenuBar could be called
> MenuBarFactoryFactory. Is this layer of abstraction important?

Yes it is.

> Would
> it be possible to instead do:
> 
> qmenubar = MenuBar(action_collection, toolkit=toolkit)

The level of abstraction is what makes it declarative. It allows you to
"declare" a whole GUI as a class or module level object. The GUI
declaration is really a structured collection of factories. When you call
it you get an actual instance of the GUI, call it again and you get another
instance.

Menus adopt the same model, although they are probably less likely to be
created declaratively as they are really an implementation detail of
actions.

> , and also to have the toolkit kwarg default to dip.ui.QtToolkit()?
> Having ui factories default to QtToolkit would simplify the code for
> the presumably most common use case.

Possibly, but in most real applications I don't think it will be an issue.
However if it turns out I get bored with typing "toolkit=" then I'll change
it.

Phil


More information about the PyQt mailing list