本文整理汇总了Python中spyderlib.qt.QtGui.QMenu.clear方法的典型用法代码示例。如果您正苦于以下问题:Python QMenu.clear方法的具体用法?Python QMenu.clear怎么用?Python QMenu.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spyderlib.qt.QtGui.QMenu
的用法示例。
在下文中一共展示了QMenu.clear方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DirView
# 需要导入模块: from spyderlib.qt.QtGui import QMenu [as 别名]
# 或者: from spyderlib.qt.QtGui.QMenu import clear [as 别名]
#.........这里部分代码省略.........
actions = []
fnames = self.get_selected_filenames()
new_actions = self.create_file_new_actions(fnames)
if len(new_actions) > 1:
# Creating a submenu only if there is more than one entry
new_act_menu = QMenu(_("New"), self)
add_actions(new_act_menu, new_actions)
actions.append(new_act_menu)
else:
actions += new_actions
import_actions = self.create_file_import_actions(fnames)
if len(import_actions) > 1:
# Creating a submenu only if there is more than one entry
import_act_menu = QMenu(_("Import"), self)
add_actions(import_act_menu, import_actions)
actions.append(import_act_menu)
else:
actions += import_actions
if actions:
actions.append(None)
if fnames:
actions += self.create_file_manage_actions(fnames)
if actions:
actions.append(None)
if fnames and all([osp.isdir(_fn) for _fn in fnames]):
actions += self.create_folder_manage_actions(fnames)
if actions:
actions.append(None)
actions += self.common_actions
return actions
def update_menu(self):
"""Update context menu"""
self.menu.clear()
add_actions(self.menu, self.create_context_menu_actions())
# ---- Events
def viewportEvent(self, event):
"""Reimplement Qt method"""
# Prevent Qt from crashing or showing warnings like:
# "QSortFilterProxyModel: index from wrong model passed to
# mapFromSource", probably due to the fact that the file system model
# is being built. See Issue 1250.
#
# This workaround was inspired by the following KDE bug:
# https://bugs.kde.org/show_bug.cgi?id=172198
#
# Apparently, this is a bug from Qt itself.
self.executeDelayedItemsLayout()
return QTreeView.viewportEvent(self, event)
def contextMenuEvent(self, event):
"""Override Qt method"""
self.update_menu()
self.menu.popup(event.globalPos())
def keyPressEvent(self, event):
"""Reimplement Qt method"""
if event.key() in (Qt.Key_Enter, Qt.Key_Return):
self.clicked()
elif event.key() == Qt.Key_F2:
self.rename()
elif event.key() == Qt.Key_Delete:
self.delete()
示例2: DirView
# 需要导入模块: from spyderlib.qt.QtGui import QMenu [as 别名]
# 或者: from spyderlib.qt.QtGui.QMenu import clear [as 别名]
#.........这里部分代码省略.........
actions = []
fnames = self.get_selected_filenames()
new_actions = self.create_file_new_actions(fnames)
if len(new_actions) > 1:
# Creating a submenu only if there is more than one entry
new_act_menu = QMenu(_('New'), self)
add_actions(new_act_menu, new_actions)
actions.append(new_act_menu)
else:
actions += new_actions
import_actions = self.create_file_import_actions(fnames)
if len(import_actions) > 1:
# Creating a submenu only if there is more than one entry
import_act_menu = QMenu(_('Import'), self)
add_actions(import_act_menu, import_actions)
actions.append(import_act_menu)
else:
actions += import_actions
if actions:
actions.append(None)
if fnames:
actions += self.create_file_manage_actions(fnames)
if actions:
actions.append(None)
if fnames and all([osp.isdir(_fn) for _fn in fnames]):
actions += self.create_folder_manage_actions(fnames)
if actions:
actions.append(None)
actions += self.common_actions
return actions
def update_menu(self):
"""Update context menu"""
self.menu.clear()
add_actions(self.menu, self.create_context_menu_actions())
#---- Events
def contextMenuEvent(self, event):
"""Override Qt method"""
self.update_menu()
self.menu.popup(event.globalPos())
def keyPressEvent(self, event):
"""Reimplement Qt method"""
if event.key() in (Qt.Key_Enter, Qt.Key_Return):
self.clicked()
elif event.key() == Qt.Key_F2:
self.rename()
elif event.key() == Qt.Key_Delete:
self.delete()
else:
QTreeView.keyPressEvent(self, event)
def mouseDoubleClickEvent(self, event):
"""Reimplement Qt method"""
QTreeView.mouseDoubleClickEvent(self, event)
self.clicked()
def clicked(self):
"""Selected item was double-clicked or enter/return was pressed"""
fnames = self.get_selected_filenames()
for fname in fnames:
if osp.isdir(fname):
self.directory_clicked(fname)
else:
self.open([fname])
示例3: OneColumnTree
# 需要导入模块: from spyderlib.qt.QtGui import QMenu [as 别名]
# 或者: from spyderlib.qt.QtGui.QMenu import clear [as 别名]
class OneColumnTree(QTreeWidget):
"""One-column tree widget with context menu, ..."""
def __init__(self, parent):
QTreeWidget.__init__(self, parent)
self.setItemsExpandable(True)
self.setColumnCount(1)
self.itemActivated.connect(self.activated)
self.itemClicked.connect(self.clicked)
# Setup context menu
self.menu = QMenu(self)
self.collapse_all_action = None
self.collapse_selection_action = None
self.expand_all_action = None
self.expand_selection_action = None
self.common_actions = self.setup_common_actions()
self.__expanded_state = None
self.itemSelectionChanged.connect(self.item_selection_changed)
self.item_selection_changed()
def activated(self, item):
"""Double-click event"""
raise NotImplementedError
def clicked(self, item):
pass
def set_title(self, title):
self.setHeaderLabels([title])
def setup_common_actions(self):
"""Setup context menu common actions"""
self.collapse_all_action = create_action(self,
text=_('Collapse all'),
icon=ima.icon('collapse'),
triggered=self.collapseAll)
self.expand_all_action = create_action(self,
text=_('Expand all'),
icon=ima.icon('expand'),
triggered=self.expandAll)
self.restore_action = create_action(self,
text=_('Restore'),
tip=_('Restore original tree layout'),
icon=ima.icon('restore'),
triggered=self.restore)
self.collapse_selection_action = create_action(self,
text=_('Collapse selection'),
icon=ima.icon('collapse_selection'),
triggered=self.collapse_selection)
self.expand_selection_action = create_action(self,
text=_('Expand selection'),
icon=ima.icon('expand_selection'),
triggered=self.expand_selection)
return [self.collapse_all_action, self.expand_all_action,
self.restore_action, None,
self.collapse_selection_action, self.expand_selection_action]
def update_menu(self):
self.menu.clear()
items = self.selectedItems()
actions = self.get_actions_from_items(items)
if actions:
actions.append(None)
actions += self.common_actions
add_actions(self.menu, actions)
def get_actions_from_items(self, items):
# Right here: add other actions if necessary
# (reimplement this method)
return []
@Slot()
def restore(self):
self.collapseAll()
for item in self.get_top_level_items():
self.expandItem(item)
def is_item_expandable(self, item):
"""To be reimplemented in child class
See example in project explorer widget"""
return True
def __expand_item(self, item):
if self.is_item_expandable(item):
self.expandItem(item)
for index in range(item.childCount()):
child = item.child(index)
self.__expand_item(child)
@Slot()
def expand_selection(self):
items = self.selectedItems()
if not items:
items = self.get_top_level_items()
for item in items:
self.__expand_item(item)
if items:
self.scrollToItem(items[0])
#.........这里部分代码省略.........
示例4: BaseTabs
# 需要导入模块: from spyderlib.qt.QtGui import QMenu [as 别名]
# 或者: from spyderlib.qt.QtGui.QMenu import clear [as 别名]
class BaseTabs(QTabWidget):
"""TabWidget with context menu and corner widgets"""
sig_close_tab = Signal(int)
def __init__(self, parent, actions=None, menu=None,
corner_widgets=None, menu_use_tooltips=False):
QTabWidget.__init__(self, parent)
self.setUsesScrollButtons(True)
# To style tabs on Mac
if sys.platform == 'darwin':
self.setObjectName('plugin-tab')
self.corner_widgets = {}
self.menu_use_tooltips = menu_use_tooltips
if menu is None:
self.menu = QMenu(self)
if actions:
add_actions(self.menu, actions)
else:
self.menu = menu
# Corner widgets
if corner_widgets is None:
corner_widgets = {}
corner_widgets.setdefault(Qt.TopLeftCorner, [])
corner_widgets.setdefault(Qt.TopRightCorner, [])
self.browse_button = create_toolbutton(self,
icon=ima.icon('browse_tab'),
tip=_("Browse tabs"))
self.browse_tabs_menu = QMenu(self)
self.browse_button.setMenu(self.browse_tabs_menu)
self.browse_button.setPopupMode(self.browse_button.InstantPopup)
self.browse_tabs_menu.aboutToShow.connect(self.update_browse_tabs_menu)
corner_widgets[Qt.TopLeftCorner] += [self.browse_button]
self.set_corner_widgets(corner_widgets)
def update_browse_tabs_menu(self):
"""Update browse tabs menu"""
self.browse_tabs_menu.clear()
names = []
dirnames = []
for index in range(self.count()):
if self.menu_use_tooltips:
text = to_text_string(self.tabToolTip(index))
else:
text = to_text_string(self.tabText(index))
names.append(text)
if osp.isfile(text):
# Testing if tab names are filenames
dirnames.append(osp.dirname(text))
offset = None
# If tab names are all filenames, removing common path:
if len(names) == len(dirnames):
common = get_common_path(dirnames)
if common is None:
offset = None
else:
offset = len(common)+1
if offset <= 3:
# Common path is not a path but a drive letter...
offset = None
for index, text in enumerate(names):
tab_action = create_action(self, text[offset:],
icon=self.tabIcon(index),
toggled=lambda state, index=index:
self.setCurrentIndex(index),
tip=self.tabToolTip(index))
tab_action.setChecked(index == self.currentIndex())
self.browse_tabs_menu.addAction(tab_action)
def set_corner_widgets(self, corner_widgets):
"""
Set tabs corner widgets
corner_widgets: dictionary of (corner, widgets)
corner: Qt.TopLeftCorner or Qt.TopRightCorner
widgets: list of widgets (may contains integers to add spacings)
"""
assert isinstance(corner_widgets, dict)
assert all(key in (Qt.TopLeftCorner, Qt.TopRightCorner)
for key in corner_widgets)
self.corner_widgets.update(corner_widgets)
for corner, widgets in list(self.corner_widgets.items()):
cwidget = QWidget()
cwidget.hide()
prev_widget = self.cornerWidget(corner)
if prev_widget:
prev_widget.close()
self.setCornerWidget(cwidget, corner)
clayout = QHBoxLayout()
clayout.setContentsMargins(0, 0, 0, 0)
for widget in widgets:
if isinstance(widget, int):
clayout.addSpacing(widget)
else:
clayout.addWidget(widget)
#.........这里部分代码省略.........