本文整理汇总了Python中PyQt5.QtWidgets.QTreeWidget.selectionModel方法的典型用法代码示例。如果您正苦于以下问题:Python QTreeWidget.selectionModel方法的具体用法?Python QTreeWidget.selectionModel怎么用?Python QTreeWidget.selectionModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QTreeWidget
的用法示例。
在下文中一共展示了QTreeWidget.selectionModel方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MiscTab
# 需要导入模块: from PyQt5.QtWidgets import QTreeWidget [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeWidget import selectionModel [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)
示例2: Preferences
# 需要导入模块: from PyQt5.QtWidgets import QTreeWidget [as 别名]
# 或者: from PyQt5.QtWidgets.QTreeWidget import selectionModel [as 别名]
class Preferences(QDialog):
configuration = {}
weight = 0
# Signals
savePreferences = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent, Qt.Dialog)
self.setWindowTitle(translations.TR_PREFERENCES_TITLE)
self.setMinimumSize(900, 650)
box = QVBoxLayout(self)
box.setContentsMargins(3, 3, 3, 3)
self.setAutoFillBackground(True)
# Header
self._header_label = QLabel("")
header_font = self._header_label.font()
header_font.setBold(True)
header_font.setPointSize(header_font.pointSize() + 4)
self._header_label.setFont(header_font)
hbox = QHBoxLayout()
hbox.setContentsMargins(0, 0, 0, 0)
self.tree = QTreeWidget()
self.tree.header().setHidden(True)
self.tree.setSelectionMode(QTreeWidget.SingleSelection)
self.tree.setAnimated(True)
self.tree.header().setSectionResizeMode(
0, QHeaderView.ResizeToContents)
self.tree.setFixedWidth(200)
hbox.addWidget(self.tree)
self.stacked = QStackedLayout()
header_layout = QVBoxLayout()
header_layout.setContentsMargins(0, 0, 0, 0)
header_layout.addWidget(self._header_label)
header_layout.addLayout(self.stacked)
hbox.addLayout(header_layout)
box.addLayout(hbox)
# Footer buttons
button_box = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
box.addWidget(button_box)
# Connections
button_box.rejected.connect(self.close)
button_box.accepted.connect(self._save_preferences)
self.tree.selectionModel().currentRowChanged.connect(
self._change_current)
self.load_ui()
@pyqtSlot()
def _save_preferences(self):
self.savePreferences.emit()
self.close()
def load_ui(self):
sections = sorted(
Preferences.configuration.keys(),
key=lambda item: Preferences.configuration[item]['weight'])
for section in sections:
text = Preferences.configuration[section]['text']
Widget = Preferences.configuration[section]['widget']
widget = Widget(self)
area = QScrollArea()
area.setWidgetResizable(True)
area.setWidget(widget)
self.stacked.addWidget(area)
index = self.stacked.indexOf(area)
item = QTreeWidgetItem([text])
item.setData(0, Qt.UserRole, index)
self.tree.addTopLevelItem(item)
# Sort Item Children
subcontent = Preferences.configuration[section].get(
'subsections', {})
subsections = sorted(
subcontent.keys(), key=lambda item: subcontent[item]['weight'])
for sub in subsections:
text = subcontent[sub]['text']
Widget = subcontent[sub]['widget']
widget = Widget(self)
area = QScrollArea()
area.setWidgetResizable(True)
area.setWidget(widget)
self.stacked.addWidget(area)
index = self.stacked.indexOf(area)
subitem = QTreeWidgetItem([text])
subitem.setData(0, Qt.UserRole, index)
item.addChild(subitem)
self.tree.expandAll()
self.tree.setCurrentIndex(self.tree.model().index(0, 0))
def _change_current(self):
item = self.tree.currentItem()
index = item.data(0, Qt.UserRole)
#.........这里部分代码省略.........