【问题标题】:PYQT5 List Widget Get Item Text [duplicate]PYQT5列表小部件获取项目文本[重复]
【发布时间】:2022-01-15 02:37:42
【问题描述】:

我有一个由 QT Designer 生成的列表小部件,我已经附加了一个函数。我的项目很长,所以我要分出一部分。如果您需要任何其他信息,请告诉我。这是一个类。

self.importedTagList.itemClicked.connect(self.tagImportListClicked)

def tagImportListClicked(self):
    print("You clicked the widget")
    print(self.importedTagList.itemActivated())

所以当我单击列表小部件上的项目时。该函数被调用,我看到“您单击了小部件”但是我似乎无法弄清楚如何从列表小部件中提取文本。我一直在查看一些示例,他们使用了 item.text() 但我似乎无法使用它。我的第二个打印功能应该如何正确显示小部件上选择的项目?

谢谢!

【问题讨论】:

标签: python-3.x pyqt5 qt-designer


【解决方案1】:

我找到了适合我的解决方案。

self.importedTagList.itemClicked.connect(self.tagImportListClicked)

def tagImportListClicked(self):
    print("You clicked the widget")
    row = self.importedTagList.currentRow()
    print(self.importedTagList.item(row).text())

我意识到我必须实际抓取该行,然后根据此选择项目。这可能不是最好的解决方案,但它目前有效。

【讨论】:

  • 绝对没有需要这样做,因为信号已经将该项目作为其参数(如documentation of itemClicked所示)。只需在tagImportListClicked 函数中添加一个item 参数,然后在此处添加print(item)
  • 感谢您的澄清。
猜你喜欢
  • 1970-01-01
  • 2014-06-06
  • 2015-02-04
  • 2015-06-09
  • 1970-01-01
  • 2018-03-28
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多