本文整理汇总了Python中PyQt5.Qt.QTreeWidget.addTopLevelItem方法的典型用法代码示例。如果您正苦于以下问题:Python QTreeWidget.addTopLevelItem方法的具体用法?Python QTreeWidget.addTopLevelItem怎么用?Python QTreeWidget.addTopLevelItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.Qt.QTreeWidget
的用法示例。
在下文中一共展示了QTreeWidget.addTopLevelItem方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ServicesQWidget
# 需要导入模块: from PyQt5.Qt import QTreeWidget [as 别名]
# 或者: from PyQt5.Qt.QTreeWidget import addTopLevelItem [as 别名]
#.........这里部分代码省略.........
"""
Update the QTreeWidget and its items
:param services: list of :class:`Services <alignak_app.items.service.Service>` items
:type services: list
"""
self.services = services
# Update services dashboard
self.services_dashboard.update_widget(self.services)
# Clear QTreeWidget
self.services_tree_widget.clear()
self.services_tree_widget.setIconSize(QSize(16, 16))
if self.services:
# Set as "Global" aggregation who are empty
for service in self.services:
if not service.data['aggregation']:
service.data['aggregation'] = 'Global'
# First sort list by state then by aggregation
newlist = sorted(
self.services,
key=lambda s: itemgetter('ls_state', 'ls_acknowledged', 'aggregation')(s.data)
)
self.services = newlist
# Get list of aggregations
aggregations = []
for service in self.services:
if service.data['aggregation'] not in aggregations:
aggregations.append(service.data['aggregation'])
# Add QTreeWidgetItems
for aggregation in aggregations:
main_tree = QTreeWidgetItem()
main_tree.setText(0, aggregation)
main_tree.setIcon(0, QIcon(settings.get_image('tree')))
main_tree.setToolTip(0, aggregation)
for service in self.services:
if service.data['aggregation'] == aggregation:
service_tree = ServiceTreeItem()
service_tree.initialize(service)
service_tree.setToolTip(0, service.get_tooltip())
self.services_tree_widget.clicked.connect(self.update_service_data)
main_tree.addChild(service_tree)
self.services_tree_widget.addTopLevelItem(main_tree)
self.service_data_widget.hide()
else:
# If no services, reset service item to None and hide data widget
self.service_data_widget.service_item = None
self.service_data_widget.hide()
def update_service_data(self): # pragma: no cover
"""
Update ServiceDataqWidget
"""
service_item = self.sender().currentItem()
if isinstance(service_item, (ServiceTreeItem, QListWidgetItem)):
service = None
# Get service
if isinstance(service_item, ServiceTreeItem):
service = data_manager.get_item('service', '_id', service_item.service_id)
elif isinstance(service_item, QListWidgetItem):
service = data_manager.get_item('service', '_id', service_item.data(Qt.UserRole))
if not service:
service = self.service_data_widget.service_item
# Update QWidgets
self.services_tree_widget.setMaximumWidth(self.width() * 0.5)
self.services_list_widget.setMaximumWidth(self.width() * 0.5)
self.service_data_widget.setMaximumWidth(self.width() * 0.5)
self.service_data_widget.update_widget(service)
self.services_dashboard.update_widget(self.services)
self.service_data_widget.show()
# Update Service Items (ServiceTreeItem, QListWidgetItem)
if isinstance(service_item, ServiceTreeItem):
service_item.update_item()
else:
monitored = \
service.data['passive_checks_enabled'] + service.data['active_checks_enabled']
icon_name = get_icon_name(
'service',
service.data['ls_state'],
service.data['ls_acknowledged'],
service.data['ls_downtimed'],
monitored
)
service_item.setData(Qt.DecorationRole, QIcon(settings.get_image(icon_name)))
service_item.setData(Qt.DisplayRole, service.get_display_name())
service_item.setToolTip(service.get_tooltip())
示例2: CollapsibleDialog
# 需要导入模块: from PyQt5.Qt import QTreeWidget [as 别名]
# 或者: from PyQt5.Qt.QTreeWidget import addTopLevelItem [as 别名]
class CollapsibleDialog(QDialog):
"""a dialog to which collapsible sections can be added;
reimplement define_sections() to define sections and
add them as (title, widget) tuples to self.sections
reimplemented from http://www.fancyaddress.com/blog/qt-2/create-something-like-the-widget-box-as-in-the-qt-designer/
"""
def __init__(self, parent = None):
super().__init__(parent)
self.tree = QTreeWidget()
self.tree.setHeaderHidden(True)
layout = QVBoxLayout()
layout.addWidget(self.tree)
self.setLayout(layout)
self.tree.setIndentation(0)
self.sections = []
self.section_dic = {}
self.define_sections()
self.add_sections()
def add_sections(self):
"""adds a collapsible sections for every
(title, widget) tuple in self.sections
"""
for (i, (title, widget)) in enumerate(self.sections):
button = self.add_button(title)
section = self.add_widget(button, widget)
button.addChild(section)
if i == 0:
button.setExpanded(True)
self.section_dic[i] = (button, section)
def define_sections(self):
"""reimplement this to define all your sections
and add them as (title, widget) tuples to self.sections
"""
widget = QFrame(self.tree)
layout = QHBoxLayout(widget)
layout.addWidget(QLabel("Bla"))
layout.addWidget(QLabel("Blubb"))
title = "Section 1"
self.sections.append((title, widget))
def add_button(self, title):
"""creates a QTreeWidgetItem containing a button
to expand or collapse its section
"""
item = QTreeWidgetItem()
self.tree.addTopLevelItem(item)
self.tree.setItemWidget(item, 0, SectionExpandButton(item, text = title))
return item
def add_widget(self, button, widget):
"""creates a QWidgetItem containing the widget,
as child of the button-QWidgetItem
"""
section = QTreeWidgetItem(button)
section.setDisabled(True)
self.tree.setItemWidget(section, 0, widget)
return section
@pyqtSlot(int, int)
def proceed_sections(self, old_section, new_section):
"""collapses old section and expands next section
"""
(button_new, _) = self.section_dic[new_section]
(button_old, _) = self.section_dic[old_section]
button_new.setExpanded(True)
button_old.setExpanded(False)
try:
self.sender().setChecked(False) # if sent from a button, un-press it
except:
pass