当前位置: 首页>>代码示例>>Python>>正文


Python QListWidget.takeItem方法代码示例

本文整理汇总了Python中PyQt5.Qt.QListWidget.takeItem方法的典型用法代码示例。如果您正苦于以下问题:Python QListWidget.takeItem方法的具体用法?Python QListWidget.takeItem怎么用?Python QListWidget.takeItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.Qt.QListWidget的用法示例。


在下文中一共展示了QListWidget.takeItem方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: EventsQWidget

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import takeItem [as 别名]

#.........这里部分代码省略.........
    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
        :type msg: str
        :return: if message exist or not in events QWidgetList
        :rtype: bool
        """

        for i in range(0, self.events_list.count()):
            if self.events_list.item(i).data(Qt.DisplayRole) == msg:
                item = self.events_list.takeItem(i)
                msg_to_send = '%s. (Send at %s)' % (msg, get_current_time())
                item.setToolTip(msg_to_send)
                self.events_list.insertItem(0, item)

                return True

        return False

    def remove_timer_event(self, event):
        """
        Remove EventItem with timer

        :param event: EventItem with timer
        :type event: EventItem
        """

        logger.debug('Remove Timer Event: %s', event.text())
        self.events_list.takeItem(self.events_list.row(event))

    def remove_event(self, item=None):
        """
        Remove item when user double click on an item

        :param item: item to remove, else remove the current row
        :type item: EventItem
        """

        if isinstance(item, EventItem):
            row = self.events_list.row(item)
            self.events_list.takeItem(row)
        else:
            item = self.events_list.takeItem(self.events_list.currentRow())

        logger.debug('Remove Event: %s', item.text())
开发者ID:Alignak-monitoring-contrib,项目名称:alignak-app,代码行数:104,代码来源:events.py

示例2: SavedSearchEditor

# 需要导入模块: from PyQt5.Qt import QListWidget [as 别名]
# 或者: from PyQt5.Qt.QListWidget import takeItem [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
#.........这里部分代码省略.........
开发者ID:JimmXinu,项目名称:calibre,代码行数:103,代码来源:saved_search_editor.py


注:本文中的PyQt5.Qt.QListWidget.takeItem方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。