qmenuview.view.MenuView

class qmenuview.view.MenuView(title='', parent=None)[source]

Bases: Mock

A view that creates submenus based on a model.

The model can be a list, table or treemodel. Each row equals to one submenu/action. In a treemodel, the leaves are plain actions, the rest also have menus on top.

The view listens to the following signals:

  • PySide.QtCore.QAbstractItemModel.modelReset
  • PySide.QtCore.QAbstractItemModel.rowsInserted
  • PySide.QtCore.QAbstractItemModel.rowsAboutToBeRemoved
  • PySide.QtCore.QAbstractItemModel.dataChanged

So the view is quite dynamic. If all child rows of an index are removed, the menu gets removed from the action. If rows are inserted to a parent index, which had no children, the action will get a menu.

If an action emits a signal, the view will emit a signal too. The signal will contain the index and any arguments of the action’s signal. You can get the action by using MenuView.get_action(). See MenuView.action_triggered, MenuView.action_hovered, MenuView.action_toggled.

Note

At the moment PySide.QtGui.QAction.changed will not be handled. There currently is a bug in MenuView._get_parents(), which causes an infinite loop.

You can set which column to use for each attribute. See MenuView.text_column, MenuView.icon_column, MenuView.icontext_column, MenuView.tooltip_column, MenuView.checked_column, MenuView.whatsthis_column, MenuView.statustip_column.

For more control on how the data gets applied to the action, change MenuView.setdataargs. It is a list of SetDataArgs containers. One container defines the functionname to use for setting the attribute, the column to use, the PySide.QtCore.Qt.ItemDataRole, and a data conversion function.

If you want custom menu and action classes, override MenuView.create_menu(), MenuView.create_action().

__init__(title='', parent=None)[source]

Initialize a new menu view with the given title

Parameters:
Raises:

None

action_hovered

Signal for when an action gets hovered

action_triggered

Signal for when an action gets triggered

action_toggled

Signal for when an action gets toggled

text_column = None

The column for the action text. Default 0

icon_column = None

The column for the action icon. Default 0

icontext_column = None

The column for the action icon text. Default -1

tooltip_column = None

The column for the tooltip data. Default 0

checked_column = None

The column for the checked data. Has to be checkable. Default 0

whatsthis_column = None

The column for the whatsThis text. Default 0

statustip_column = None

The column for the statustip text. Default 0

setdataargs = None

A list of SetDataArgs containers. Defines how the data from the model is applied to the action

model

Get the model

Returns:the current model
Return type:PySide.QtCore.QAbstractItemModel
Raises:None
reset()[source]

Delete and recreate all menus

Returns:None
Return type:None
Raises:None
create_all_menus()[source]

Create all menus according to the model

Returns:None
Return type:None
Raises:None
create_menu_for_index(index)[source]
create_menu(parent)[source]

Create a menu and return the menus action.

The parent of the menu has to be set to parent

Parameters:parent (PySide.QtGui.QMenu) – The parent menu
Returns:The menu action
Return type:PySide.QtGui.QAction
Raises:None
create_action(parent)[source]

Create and return a new action

The parent of the action has to be set to parent

Parameters:parent (PySide.QtGui.QMenu) – The parent menu
Returns:The created action
Return type:PySide.QtGui.QAction
Raises:None
insert_menus(parent, first, last)[source]

Create menus for rows first til last under the given parent

Parameters:
Returns:

None

Return type:

None

Raises:

None

remove_menus(parent, first, last)[source]

Remove the menus under the given parent

Parameters:
Returns:

None

Return type:

None

Raises:

None

update_menus(topLeft, bottomRight)[source]

Update the menus from topleft index to bottomright index

Parameters:
Returns:

None

Return type:

None

Raises:

None

get_index(action, column=0)[source]

Return the index for the given action

Parameters:
Returns:

the index of the action

Return type:

PySide.QtCore.QModelIndex

Rasies:

None

get_action(index)[source]

Return the action for the given index

Parameters:index (PySide.QtCore.QModelIndex) – the index to query
Returns:the action for the given index
Return type:PySide.QtGui.QAction
Raises:None
set_action_data(action, index)[source]

Set the data of the action for the given index

Note

The column of the index does not matter. The columns for the data are specified in MenuView.setdataargs.

The arguments to used are defined in MenuView.setdataargs.

Parameters:
Returns:

None

Return type:

None

Raises:

None

static get_data(index, role, column=None)[source]

Get data of the given index

If the column is is not None and different from the index column, will get the data from a sibling index with the same row.

Parameters:
  • index (PySide.QtCore.QModelIndex) – The index to query for data
  • role (PySide.QtCore.Qt.ItemDataRole) – the data role
  • column (int | None) – the column of the row to query for data.
Returns:

The data retrieved

Raises:

None