本文整理汇总了Python中PyQt5.Qt.QListWidget.insertItem方法的典型用法代码示例。如果您正苦于以下问题:Python QListWidget.insertItem方法的具体用法?Python QListWidget.insertItem怎么用?Python QListWidget.insertItem使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.Qt.QListWidget
的用法示例。
在下文中一共展示了QListWidget.insertItem方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EventsQWidget
# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import insertItem [as 别名]
class EventsQWidget(QWidget):
"""
Class who create QWidget for events
"""
def __init__(self):
super(EventsQWidget, self).__init__()
self.setObjectName('events')
# Fields
self.events_list = QListWidget()
self.timer = QTimer()
def initialize(self):
"""
Intialize QWidget
"""
self.timer.setInterval(30000)
self.timer.start()
self.timer.timeout.connect(self.send_datamanager_events)
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
self.setLayout(layout)
self.events_list.setDragDropMode(QAbstractItemView.DragOnly)
self.events_list.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.events_list.setDropIndicatorShown(True)
self.events_list.doubleClicked.connect(self.remove_event)
self.events_list.setWordWrap(True)
self.events_list.setIconSize(QSize(16, 16))
self.add_event(
'OK',
_('Welcome %s, you are connected to Alignak Backend') %
data_manager.database['user'].name,
timer=True
)
layout.addWidget(self.events_list)
def send_datamanager_events(self):
"""
Add events stored in DataManager
"""
events = data_manager.get_events()
if events:
for event in events:
self.add_event(
event['event_type'],
event['message'],
timer=False,
host=event['host']
)
def add_event(self, event_type, msg, timer=False, host=None):
"""
Add event to events list
:param event_type: the type of event: OK, DOWN, ACK, ...
:type event_type: str
:param msg: message of event
:type msg: str
:param timer: timer to hide event at end of time
:type timer: bool
:param host: data of a host to set ``Qt.UserRole``
:type host: None | str
"""
if not self.event_exist(msg):
logger.debug(
'Add Event: msg: %s, timer: %s, host: %s', msg, timer, host
)
event = EventItem()
event.initialize(event_type, msg, timer=timer, host=host)
self.events_list.insertItem(0, event)
if timer:
event_duration = int(
settings.get_config('Alignak-app', 'notification_duration')
) * 1000
QTimer.singleShot(
event_duration,
lambda: self.remove_timer_event(event)
)
else:
logger.debug(
'Event with msg: %s already exist.', msg
)
def event_exist(self, msg):
"""
Check if event already displayed, move it to top and update tooltip.
Only for EventItem who have a ``Qt.UserRole``
:param msg: message of event
#.........这里部分代码省略.........
示例2: SpyQWidget
# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import insertItem [as 别名]
class SpyQWidget(QWidget):
"""
Class who create QWidget for spied hosts
"""
def __init__(self):
super(SpyQWidget, self).__init__()
self.host_services_lbl = QLabel(_('You are not spying on any hosts for now...'))
self.spy_list_widget = SpyQListWidget()
self.host_list_widget = QListWidget()
self.spy_timer = QTimer()
def initialize(self):
"""
Intialize Spy QWidget
"""
layout = QGridLayout()
self.setLayout(layout)
spy_icon = QLabel()
spy_pixmap = QPixmap(settings.get_image('spy'))
spy_icon.setPixmap(spy_pixmap)
spy_icon.setScaledContents(True)
spy_icon.setFixedSize(20, 20)
layout.addWidget(spy_icon, 0, 0, 1, 1)
layout.setAlignment(spy_icon, Qt.AlignRight)
spy_title = QLabel(_('Spy Hosts'))
spy_title.setObjectName('title')
spy_title.setMinimumHeight(40)
layout.addWidget(spy_title, 0, 1, 1, 1)
hint_lbl = QLabel('Click to refresh, double-click to stop spying')
hint_lbl.setObjectName('subtitle')
layout.addWidget(hint_lbl, 1, 0, 1, 1)
layout.setAlignment(hint_lbl, Qt.AlignCenter)
self.host_services_lbl.setObjectName('subtitle')
layout.addWidget(self.host_services_lbl, 1, 1, 1, 1)
layout.setAlignment(self.host_services_lbl, Qt.AlignCenter)
self.spy_list_widget.setDragDropMode(QAbstractItemView.DragDrop)
self.spy_list_widget.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.spy_list_widget.doubleClicked.connect(self.remove_event)
self.spy_list_widget.setAcceptDrops(True)
self.spy_list_widget.setWordWrap(True)
self.spy_list_widget.insertItem(0, self.get_hint_item())
self.spy_list_widget.item_dropped.connect(get_events_widget().remove_event)
self.spy_list_widget.clicked.connect(
lambda: self.manage_host_events(self.spy_list_widget.currentRow())
)
layout.addWidget(self.spy_list_widget, 2, 0, 1, 1)
self.host_list_widget.setObjectName('spy')
# self.host_list_widget.setMinimumWidth(500)
layout.addWidget(self.host_list_widget, 2, 1, 1, 1)
spy_interval = int(settings.get_config('Alignak-app', 'spy_interval')) * 1000
self.spy_timer.setInterval(spy_interval)
self.spy_timer.start()
self.spy_timer.timeout.connect(self.send_spy_events)
@staticmethod
def get_hint_item():
"""
Return an EventItem with a hint text
:return: event item with hint text
:rtype: EventItem
"""
drop_hint_item = EventItem()
drop_hint_item.setText(_('Drop host-related events here to spy on it...'))
drop_hint_item.setIcon(QIcon(settings.get_image('spy')))
drop_hint_item.setFlags(Qt.ItemIsDropEnabled)
return drop_hint_item
def send_spy_events(self):
"""
Send event messages for all hosts who are spied
"""
if self.spy_list_widget.spied_hosts:
for host_id in self.spy_list_widget.spied_hosts:
host = data_manager.get_item('host', host_id)
get_events_widget().add_event(
EventItem.get_event_type(host.data),
_('Host %s, current state: %s') % (
host.get_display_name(), host.data['ls_state']),
host=host.item_id
)
def manage_host_events(self, row):
"""
Manage spy events for a host, defined by current row of "spy_list_widget"
#.........这里部分代码省略.........
示例3: SavedSearchEditor
# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import insertItem [as 别名]
class SavedSearchEditor(Dialog):
def __init__(self, parent, initial_search=None):
self.initial_search = initial_search
Dialog.__init__(
self, _('Manage saved searches'), 'manage-saved-searches', parent)
def setup_ui(self):
from calibre.gui2.ui import get_gui
db = get_gui().current_db
self.l = l = QVBoxLayout(self)
b = self.bb.addButton(_('&Add search'), self.bb.ActionRole)
b.setIcon(QIcon(I('plus.png')))
b.clicked.connect(self.add_search)
b = self.bb.addButton(_('&Remove search'), self.bb.ActionRole)
b.setIcon(QIcon(I('minus.png')))
b.clicked.connect(self.del_search)
b = self.bb.addButton(_('&Edit search'), self.bb.ActionRole)
b.setIcon(QIcon(I('modified.png')))
b.clicked.connect(self.edit_search)
self.slist = QListWidget(self)
self.slist.setStyleSheet('QListView::item { padding: 3px }')
self.slist.activated.connect(self.edit_search)
self.slist.setAlternatingRowColors(True)
self.searches = {name: db.saved_search_lookup(name) for name in db.saved_search_names()}
self.populate_search_list()
if self.initial_search is not None and self.initial_search in self.searches:
self.select_search(self.initial_search)
elif self.searches:
self.slist.setCurrentRow(0)
self.slist.currentItemChanged.connect(self.current_index_changed)
l.addWidget(self.slist)
self.desc = la = QLabel('\xa0')
la.setWordWrap(True)
l.addWidget(la)
l.addWidget(self.bb)
self.current_index_changed(self.slist.currentItem())
self.setMinimumHeight(500)
self.setMinimumWidth(600)
@property
def current_search_name(self):
i = self.slist.currentItem()
if i is not None:
ans = i.text()
if ans in self.searches:
return ans
def populate_search_list(self):
self.slist.clear()
for name in sorted(self.searches.keys(), key=sort_key):
self.slist.addItem(name)
def add_search(self):
d = AddSavedSearch(parent=self, commit_changes=False)
if d.exec_() != d.Accepted:
return
name, expression = d.accepted_data
nmap = {icu_lower(n):n for n in self.searches}
if icu_lower(name) in nmap:
q = nmap[icu_lower(name)]
del self.searches[q]
self.select_search(q)
self.slist.takeItem(self.slist.currentRow())
self.searches[name] = expression
self.slist.insertItem(0, name)
self.slist.setCurrentRow(0)
self.current_index_changed(self.slist.currentItem())
def del_search(self):
n = self.current_search_name
if n is not None:
if not confirm(
'<p>' + _(
'The current saved search will be '
'<b>permanently deleted</b>. Are you sure?') + '</p>',
'saved_search_editor_delete', self):
return
self.slist.takeItem(self.slist.currentRow())
del self.searches[n]
def edit_search(self):
n = self.current_search_name
if not n:
return
d = AddSavedSearch(parent=self, commit_changes=False, label=_('Edit the name and/or expression below.'), validate=self.validate_edit)
d.setWindowTitle(_('Edit saved search'))
d.sname.setText(n)
d.search.setText(self.searches[n])
if d.exec_() != d.Accepted:
return
name, expression = d.accepted_data
self.slist.currentItem().setText(name)
del self.searches[n]
self.searches[name] = expression
#.........这里部分代码省略.........