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()
. SeeMenuView.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 inMenuView._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 ofSetDataArgs
containers. One container defines the functionname to use for setting the attribute, the column to use, thePySide.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: - title (
str
) – title of the top menu - parent (
PySide.QtGui.QWidget
) – the parent widget
Raises: None
- title (
-
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
-
create_all_menus
()[source]¶ Create all menus according to the model
Returns: None Return type: None Raises: None
-
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 menuReturns: 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 menuReturns: 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: - parent (
PySide.QtCore.QModelIndex
) – The parent index - first (
int
) – the first row - last (
int
) – the last row
Returns: None
Return type: Raises: None
- parent (
-
remove_menus
(parent, first, last)[source]¶ Remove the menus under the given parent
Parameters: - parent (
PySide.QtCore.QModelIndex
) – the parent of the menus - first (
int
) – the first row - last (
int
) – the last row
Returns: None
Return type: Raises: None
- parent (
-
update_menus
(topLeft, bottomRight)[source]¶ Update the menus from topleft index to bottomright index
Parameters: - topLeft (
PySide.QtCore.QModelIndex
) – The top left index to update - bottomRight (
PySide.QtCore.QModelIndex
) – the bottom right index to update
Returns: None
Return type: Raises: None
- topLeft (
-
get_index
(action, column=0)[source]¶ Return the index for the given action
Parameters: - action (
PySide.QtGui.QAction
) – the action to query - column (
int
) – The column of the index
Returns: the index of the action
Return type: Rasies: None
- action (
-
get_action
(index)[source]¶ Return the action for the given index
Parameters: index ( PySide.QtCore.QModelIndex
) – the index to queryReturns: 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: - action (
PySide.QtGui.QAction
) – The action to update - index (
PySide.QtCore.QModelIndex
) – The index with the data
Returns: None
Return type: Raises: None
- action (
-
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
- index (