本文整理汇总了Python中PyQt5.QtWidgets.QTreeWidget.setItemWidget方法的典型用法代码示例。如果您正苦于以下问题:Python QTreeWidget.setItemWidget方法的具体用法?Python QTreeWidget.setItemWidget怎么用?Python QTreeWidget.setItemWidget使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QTreeWidget
的用法示例。
在下文中一共展示了QTreeWidget.setItemWidget方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from PyQt5.QtWidgets import QTreeWidget [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeWidget import setItemWidget [as 别名]
def __init__(self, *args, **kwargs):
super(ShortcutManagerDlg, self).__init__(*args, **kwargs)
self.setWindowTitle("Shortcut Preferences")
self.setMinimumWidth(500)
self.setMinimumHeight(500)
mgr = ShortcutManager() # Singleton
scrollWidget = QWidget(parent=self)
tempLayout = QVBoxLayout(scrollWidget)
scrollWidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
treeWidget = QTreeWidget(parent=scrollWidget)
treeWidget.setHeaderLabels(["Action", "Shortcut"])
treeWidget.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
treeWidget.setColumnWidth(0, 300)
treeWidget.setColumnWidth(1, 50)
action_descriptions = mgr.get_all_action_descriptions()
target_keyseqs = mgr.get_keyseq_reversemap()
# Create a LineEdit for each shortcut,
# and keep track of them in a dict
shortcutEdits = collections.OrderedDict()
for group, targets in list(action_descriptions.items()):
groupItem = QTreeWidgetItem(treeWidget, [group])
for (name, description) in targets:
edit = QLineEdit(target_keyseqs[(group, name)])
shortcutEdits[(group, name)] = edit
item = QTreeWidgetItem(groupItem, [description])
item.setText(0, description)
treeWidget.setItemWidget(item, 1, edit)
tempLayout.addWidget(treeWidget)
# Add ok and cancel buttons
buttonLayout = QHBoxLayout()
cancelButton = QPushButton("Cancel")
cancelButton.clicked.connect(self.reject)
okButton = QPushButton("OK")
okButton.clicked.connect(self.accept)
okButton.setDefault(True)
buttonLayout.addSpacerItem(QSpacerItem(10, 0, QSizePolicy.Expanding))
buttonLayout.addWidget(cancelButton)
buttonLayout.addWidget(okButton)
tempLayout.addLayout(buttonLayout)
scroll = QScrollArea(parent=self)
scroll.setWidget(scrollWidget)
scroll.setWidgetResizable(True)
scroll.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
dlgLayout = QVBoxLayout()
dlgLayout.addWidget(scroll)
self.setLayout(dlgLayout)
# Show the window
result = self.exec_()
# Did the user hit 'cancel'?
if result != QDialog.Accepted:
return
for (group, name), edit in list(shortcutEdits.items()):
oldKey = target_keyseqs[(group, name)]
newKey = str(edit.text())
if oldKey.lower() != newKey.lower() and newKey != "":
mgr.change_keyseq(group, name, oldKey, newKey)
mgr.store_to_preferences()
示例2: MiscTab
# 需要导入模块: from PyQt5.QtWidgets import QTreeWidget [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeWidget import setItemWidget [as 别名]
class MiscTab(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.name = self.tr("Misc")
self.markColorLabel = QLabel(self.tr("Default flag colors:"), self)
# TODO: enforce duplicate names avoidance
self.markColorWidget = QTreeWidget(self)
self.markColorWidget.setHeaderLabels(
(self.tr("Color"), self.tr("Name")))
self.markColorWidget.setRootIsDecorated(False)
self.markColorWidget.setSelectionBehavior(
QAbstractItemView.SelectRows)
# TODO: make this work correctly, top-level items only
# self.markColorWidget.setDragDropMode(QAbstractItemView.InternalMove)
self.addItemButton = QPushButton("+", self)
self.addItemButton.clicked.connect(self.addItem)
self.removeItemButton = QPushButton("−", self)
self.removeItemButton.clicked.connect(self.removeItem)
self.loadRecentFileBox = QCheckBox(
self.tr("Load most recent file on start"), self)
layout = QGridLayout(self)
l = 0
layout.addWidget(self.markColorLabel, l, 0, 1, 3)
l += 1
layout.addWidget(self.markColorWidget, l, 0, 1, 3)
l += 1
layout.addWidget(self.addItemButton, l, 0)
layout.addWidget(self.removeItemButton, l, 1)
l += 1
layout.addWidget(self.loadRecentFileBox, l, 0, 1, 3)
self.setLayout(layout)
self.readSettings()
def addItem(self):
def mangleNewName():
name = self.tr("New")
index = 0
while self.markColorWidget.findItems(name, Qt.MatchExactly, 1):
index += 1
name = "{0} ({1})".format(name, index)
return name
# TODO: not DRY with ctor
item = QTreeWidgetItem(self.markColorWidget)
item.setFlags(item.flags() | Qt.ItemIsEditable)
widget = ColorVignette(self)
widget.setColor(QColor(Qt.white))
widget.setMargins(2, 2, 2, 2)
widget.setMayClearColor(False)
self.markColorWidget.setItemWidget(item, 0, widget)
item.setText(1, mangleNewName())
self.markColorWidget.setCurrentItem(item)
self.markColorWidget.editItem(item, 1)
self.removeItemButton.setEnabled(True)
def removeItem(self):
i = self.markColorWidget.selectionModel().currentIndex().row()
self.markColorWidget.takeTopLevelItem(i)
if not self.markColorWidget.topLevelItemCount():
self.removeItemButton.setEnabled(False)
def readSettings(self):
entries = settings.readMarkColors()
for name, color in entries.items():
item = QTreeWidgetItem(self.markColorWidget)
item.setFlags(item.flags() | Qt.ItemIsEditable)
widget = ColorVignette(self)
widget.setColor(color)
widget.setMargins(2, 2, 2, 2)
widget.setMayClearColor(False)
self.markColorWidget.setItemWidget(item, 0, widget)
item.setText(1, name)
if not len(entries):
self.removeItemButton.setEnabled(False)
loadRecentFile = settings.loadRecentFile()
self.loadRecentFileBox.setChecked(loadRecentFile)
def writeSettings(self):
markColors = OrderedDict()
for i in range(self.markColorWidget.topLevelItemCount()):
item = self.markColorWidget.topLevelItem(i)
name = item.text(1)
color = self.markColorWidget.itemWidget(item, 0).color()
markColors[name] = color
settings.writeMarkColors(markColors)
loadRecentFile = self.loadRecentFileBox.isChecked()
settings.setLoadRecentFile(loadRecentFile)
示例3: InspectorWindow
# 需要导入模块: from PyQt5.QtWidgets import QTreeWidget [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeWidget import setItemWidget [as 别名]
#.........这里部分代码省略.........
if self._glyph.rightMargin is not None:
rightSideBearing = str(int(self._glyph.rightMargin))
if self._glyph.markColor is not None:
markColor = QColor.fromRgbF(
*tuple(self._glyph.markColor))
self.nameEdit.setText(name)
self.unicodesEdit.setText(unicodes)
self.widthEdit.setText(width)
self.leftSideBearingEdit.setText(leftSideBearing)
self.rightSideBearingEdit.setText(rightSideBearing)
self.markColorWidget.setColor(markColor)
def _updateLayerAttributes(self, notification=None):
self.layerSetWidget.clear()
if self._font is None:
return
layerSet = self._font.layers
if layerSet is None:
return
for layer in layerSet:
item = QTreeWidgetItem(self.layerSetWidget)
item.setFlags(item.flags() | Qt.ItemIsEditable)
item.setText(0, layer.name)
widget = ColorVignette(self)
color = layer.color
if color is not None:
color = QColor.fromRgbF(*tuple(color))
widget.setColor(color)
widget.setMargins(2, 2, 2, 2)
widget.setMayClearColor(False)
widget.colorChanged.connect(self.writeLayerColor)
widget.setProperty("layer", layer)
self.layerSetWidget.setItemWidget(item, 1, widget)
self.layerSetWidget.setColumnWidth(1, 100)
# ---------
# Callbacks
# ---------
# glyph attributes
def writeGlyphName(self):
if self._glyph is None:
return
self._glyph.name = self.nameEdit.text()
def writeUnicodes(self):
if self._glyph is None:
return
unicodes = self.unicodesEdit.text().split(" ")
if len(unicodes) == 1 and unicodes[0] == "":
self._glyph.unicodes = []
else:
self._glyph.unicodes = [int(uni, 16) for uni in unicodes]
def writeWidth(self):
if self._glyph is None:
return
self._glyph.width = int(self.widthEdit.text())
def writeLeftSideBearing(self):
if self._glyph is None:
return
self._glyph.leftMargin = int(self.leftSideBearingEdit.text())