There was a task to display the existing list in the PyQt widget window so that the list items were clickable and you could attach a click event handler to each one. Everywhere I find examples of a handler for clicking a button, but I need for list items. Tell me, can I do this and where to look?

  • one
    Dig in the direction of QListWidget, or QListView - Alexander Chernin
  • one
    You can also see QTableWidget, because it has a setCellWidget method, which makes it possible to stick any widget into a cell - Alexander Chernin
  • Thanks for the answers) - ZaurK

1 answer 1

If I understand you correctly, you need a signal itemClicked

 import sys from PyQt5.QtWidgets import (QWidget, QListWidget, QVBoxLayout, QApplication) LISTS = ("item1", "item2", "item3", "item4", "item5",) class Example(QWidget): def __init__(self): super().__init__() self.l = QListWidget() self.l.addItems(LISTS) self.l.itemClicked.connect(self.selectionChanged) vbox = QVBoxLayout() vbox.addWidget(self.l) self.setLayout(vbox) def selectionChanged(self, item): print("Вы кликнули: {}".format(item.text())) if item.text()=="item2": print("Делайте что-нибудь.") # ... if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() ex.show() sys.exit(app.exec_()) 

enter image description here

  • S. Nick, thank you very much, they helped out straight :) - ZaurK