【问题标题】:How to insert Items from a list into QlistWidget?如何将列表中的项目插入 QlistWidget?
【发布时间】:2020-09-18 20:59:15
【问题描述】:

我想在 QlistWidget 中插入一个项目列表。 以下是我正在使用的代码:

names = ['apple', 'banana', 'Cherry']
for item in names:
   self.listWidget.insertItems(item)

但我遇到以下错误:

TypeError: insertItems(self, int, Iterable[str]): 参数 1 有 意外类型“str”

请让我知道问题所在。

【问题讨论】:

    标签: python python-3.x pyqt pyqt5 qlistwidget


    【解决方案1】:

    如果你检查the docs

    void QListWidget::insertItems(int row, const QStringList &labels)

    将标签列表中的项目插入到列表中,从 给定行。

    据观察,方法 X 需要具有将要插入的初始位置,因此当未提供要添加的信息时,将显示 2 个解决方案:

    在开头添加:

    self.listWidget.insertItems(0, names)
    

    在末尾添加:

    self.listWidget.insertItems(self.listWidget.count(), names)
    

    对于最后一种情况,最好使用addItems() 方法:

    self.listWidget.addItems(names)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      相关资源
      最近更新 更多