本文整理汇总了Python中PyQt5.QtWidgets.QTreeWidget.setContextMenuPolicy方法的典型用法代码示例。如果您正苦于以下问题:Python QTreeWidget.setContextMenuPolicy方法的具体用法?Python QTreeWidget.setContextMenuPolicy怎么用?Python QTreeWidget.setContextMenuPolicy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QTreeWidget
的用法示例。
在下文中一共展示了QTreeWidget.setContextMenuPolicy方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SqlConnectionWidget
# 需要导入模块: from PyQt5.QtWidgets import QTreeWidget [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeWidget import setContextMenuPolicy [as 别名]
class SqlConnectionWidget(QWidget):
"""
Class implementing a widget showing the SQL connections.
@signal tableActivated(str) emitted after the entry for a table has been
activated
@signal schemaRequested(str) emitted when the schema display is requested
@signal cleared() emitted after the connection tree has been cleared
"""
tableActivated = pyqtSignal(str)
schemaRequested = pyqtSignal(str)
cleared = pyqtSignal()
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget (QWidget)
"""
super(SqlConnectionWidget, self).__init__(parent)
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
self.__connectionTree = QTreeWidget(self)
self.__connectionTree.setObjectName("connectionTree")
self.__connectionTree.setHeaderLabels([self.tr("Database")])
if qVersion() >= "5.0.0":
self.__connectionTree.header().setSectionResizeMode(
QHeaderView.Stretch)
else:
self.__connectionTree.header().setResizeMode(QHeaderView.Stretch)
refreshAction = QAction(self.tr("Refresh"), self.__connectionTree)
self.__schemaAction = QAction(
self.tr("Show Schema"), self.__connectionTree)
refreshAction.triggered.connect(self.refresh)
self.__schemaAction.triggered.connect(self.showSchema)
self.__connectionTree.addAction(refreshAction)
self.__connectionTree.addAction(self.__schemaAction)
self.__connectionTree.setContextMenuPolicy(Qt.ActionsContextMenu)
layout.addWidget(self.__connectionTree)
self.__activating = False
self.__connectionTree.itemActivated.connect(self.__itemActivated)
self.__connectionTree.currentItemChanged.connect(
self.__currentItemChanged)
self.__activeDb = ""
def refresh(self):
"""
Public slot to refresh the connection tree.
"""
self.__connectionTree.clear()
self.cleared.emit()
connectionNames = QSqlDatabase.connectionNames()
foundActiveDb = False
for name in connectionNames:
root = QTreeWidgetItem(self.__connectionTree)
db = QSqlDatabase.database(name, False)
root.setText(0, self.__dbCaption(db))
if name == self.__activeDb:
foundActiveDb = True
self.__setActive(root)
if db.isOpen():
tables = db.tables()
for table in tables:
itm = QTreeWidgetItem(root)
itm.setText(0, table)
if not foundActiveDb and connectionNames:
self.__activeDb = connectionNames[0]
self.__setActive(self.__connectionTree.topLevelItem(0))
def showSchema(self):
"""
Public slot to show schema data of a database.
"""
cItm = self.__connectionTree.currentItem()
if cItm is None or cItm.parent() is None:
return
self.__setActive(cItm.parent())
self.schemaRequested.emit(cItm.text(0))
def __itemActivated(self, itm, column):
"""
Private slot handling the activation of an item.
@param itm reference to the item (QTreeWidgetItem)
@param column column that was activated (integer)
"""
if itm is None:
return
#.........这里部分代码省略.........
示例2: BookmarksWindow
# 需要导入模块: from PyQt5.QtWidgets import QTreeWidget [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeWidget import setContextMenuPolicy [as 别名]
class BookmarksWindow(QDialog):
"""
A simple UI for showing bookmarks and navigating to them.
FIXME: For now, this window is tied to a particular lane.
If your project has more than one lane, then each one
will have it's own bookmark window, which is kinda dumb.
"""
def __init__(self, parent, topLevelOperatorView):
super(BookmarksWindow, self).__init__(parent)
self.setWindowTitle("Bookmarks")
self.topLevelOperatorView = topLevelOperatorView
self.bookmark_tree = QTreeWidget(self)
self.bookmark_tree.setHeaderLabels( ["Location", "Notes"] )
self.bookmark_tree.setSizePolicy( QSizePolicy.Preferred, QSizePolicy.Preferred )
self.bookmark_tree.setColumnWidth(0, 200)
self.bookmark_tree.setColumnWidth(1, 300)
self.note_edit = QLineEdit(self)
self.add_bookmark_button = QPushButton("Add Bookmark", self, clicked=self.add_bookmark)
geometry = self.geometry()
geometry.setSize( QSize(520, 520) )
self.setGeometry(geometry)
layout = QVBoxLayout()
layout.addWidget(self.bookmark_tree)
layout.addWidget(self.note_edit)
layout.addWidget(self.add_bookmark_button)
self.setLayout(layout)
self._load_bookmarks()
self.bookmark_tree.setContextMenuPolicy( Qt.CustomContextMenu )
self.bookmark_tree.customContextMenuRequested.connect( self.showContextMenu )
self.bookmark_tree.itemDoubleClicked.connect(self._handle_doubleclick)
def _handle_doubleclick(self, item, col):
"""
Navigate to the bookmark
"""
data = item.data(0, Qt.UserRole).toPyObject()
if data is None:
return
(coord, notes) = data
axes = self.topLevelOperatorView.InputImages.meta.getAxisKeys()
axes = axes[:-1] # drop channel
axes = sorted(axes)
assert len(axes) == len(coord)
tagged_coord = dict(list(zip(axes, coord)))
tagged_location = OrderedDict(list(zip('txyzc', (0,0,0,0,0))))
tagged_location.update(tagged_coord)
t = list(tagged_location.values())[0]
coord3d = list(tagged_location.values())[1:4]
self.parent().editor.posModel.time = t
self.parent().editor.navCtrl.panSlicingViews( coord3d, [0,1,2] )
self.parent().editor.posModel.slicingPos = coord3d
def showContextMenu(self, pos):
item = self.bookmark_tree.itemAt(pos)
data = item.data(0, Qt.UserRole).toPyObject()
if data is None:
return
def delete_bookmark():
(coord, notes) = data
bookmarks = list(self.topLevelOperatorView.Bookmarks.value)
i = bookmarks.index((coord, notes))
bookmarks.pop(i)
self.topLevelOperatorView.Bookmarks.setValue(bookmarks)
self._load_bookmarks()
menu = QMenu(parent=self)
menu.addAction( QAction("Delete", menu, triggered=delete_bookmark) )
globalPos = self.bookmark_tree.viewport().mapToGlobal( pos )
menu.exec_( globalPos )
#selection = menu.exec_( globalPos )
#if selection is removeLanesAction:
# self.removeLanesRequested.emit( self._selectedLanes )
def add_bookmark(self):
coord_txyzc = self.parent().editor.posModel.slicingPos5D
tagged_coord_txyzc = dict( list(zip('txyzc', coord_txyzc)) )
axes = self.topLevelOperatorView.InputImages.meta.getAxisKeys()
axes = axes[:-1] # drop channel
axes = sorted(axes)
coord = tuple(tagged_coord_txyzc[c] for c in axes)
notes = str(self.note_edit.text())
bookmarks = list(self.topLevelOperatorView.Bookmarks.value)
bookmarks.append((coord, notes))
self.topLevelOperatorView.Bookmarks.setValue(bookmarks)
self._load_bookmarks()
def _load_bookmarks(self):
self.bookmark_tree.clear()
#.........这里部分代码省略.........
示例3: PugdebugExpressionViewer
# 需要导入模块: from PyQt5.QtWidgets import QTreeWidget [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeWidget import setContextMenuPolicy [as 别名]
class PugdebugExpressionViewer(QWidget):
expression_added_signal = pyqtSignal(int, str)
expression_changed_signal = pyqtSignal(int, str)
def __init__(self):
super(PugdebugExpressionViewer, self).__init__()
# Action for adding a new expression
self.add_action = QAction(QIcon.fromTheme('list-add'), "&Add", self)
self.add_action.triggered.connect(self.handle_add_action)
# Action for deleting selected expressions
self.delete_action = QAction(
QIcon.fromTheme('list-remove'),
"&Delete", self
)
self.delete_action.setShortcut(QKeySequence("Del"))
self.delete_action.triggered.connect(self.handle_delete_action)
self.toolbar = QToolBar()
self.toolbar.setIconSize(QSize(16, 16))
self.toolbar.addAction(self.add_action)
self.toolbar.addAction(self.delete_action)
self.tree = QTreeWidget()
self.tree.setColumnCount(3)
self.tree.setHeaderLabels(['Expression', 'Type', 'Value'])
self.tree.setSelectionMode(QAbstractItemView.ContiguousSelection)
self.tree.setContextMenuPolicy(Qt.CustomContextMenu)
self.tree.customContextMenuRequested.connect(self.show_context_menu)
layout = QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.tree)
self.setLayout(layout)
self.restore_state()
self.tree.itemChanged.connect(self.handle_item_changed)
def show_context_menu(self, point):
# Create the context menu on the tree widget
context_menu = QMenu(self)
# Add action is always visible
context_menu.addAction(self.add_action)
# If clicked on an row, offer to delete it
if self.tree.itemAt(point):
context_menu.addAction(self.delete_action)
point = self.tree.mapToGlobal(point)
context_menu.popup(point)
def add_expression(self, expression):
item = QTreeWidgetItem([expression, '', ''])
item.setFlags(
Qt.ItemIsEnabled |
Qt.ItemIsEditable |
Qt.ItemIsSelectable
)
self.tree.addTopLevelItem(item)
# Emit the signal to evaluate the expression
index = self.tree.indexOfTopLevelItem(item)
self.expression_added_signal.emit(index, expression)
def delete_selected(self):
"""Deletes currently selected items from the tree"""
for item in self.tree.selectedItems():
index = self.tree.indexOfTopLevelItem(item)
self.delete_expression(item)
self.select_next(index)
def select_next(self, index):
"""Selects the next item after an item has been deleted"""
prev_item = self.tree.topLevelItem(index - 1)
next_item = self.tree.topLevelItem(index)
if prev_item:
prev_item.setSelected(True)
elif next_item:
next_item.setSelected(True)
def clear_values(self):
"""Deletes values for all expressions"""
count = self.tree.topLevelItemCount()
for index in range(0, count):
item = self.tree.topLevelItem(index)
item.setData(1, Qt.DisplayRole, '')
item.setData(2, Qt.DisplayRole, '')
item.takeChildren()
def delete_expression(self, item):
"""Deletes the given item from the tree"""
index = self.tree.indexOfTopLevelItem(item)
self.tree.takeTopLevelItem(index)
def get_expressions(self):
#.........这里部分代码省略.........