Like song list and file list, this can be implemented with QListView, for example, the following results:
The code is as follows:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListView, QMessageBox from PyQt5.QtCore import QStringListModel import sys class ListViewDemo(QWidget): def __init__(self, parent=None): super(ListViewDemo, self).__init__(parent) self.setWindowTitle("QListView Example") self.resize(300, 270) layout = QVBoxLayout() listview = QListView() listModel = QStringListModel() self.list = ["List item 1","List item 2", "List item 3"] listModel.setStringList(self.list) listview.setModel(listModel) listview.clicked.connect(self.onClickedListView) layout.addWidget(listview) self.setLayout(layout) def onClickedListView(self,item): QMessageBox.information(self,"QListView","You have chosen:" + self.list[item.row()]) if __name__ == "__main__": app = QApplication(sys.argv) win = ListViewDemo() win.show() sys.exit(app.exec_())
Clicking an item triggers the signal clicked, which is as follows:
def clicked(self, QModelIndex): # real signature unknown; restored from __doc__ """ clicked(self, QModelIndex) [signal] """ pass
Qmodeldindex saves the row information of the current click. You can get the current item index through row().
The code above links the signal
listview.clicked.connect(self.onClickedListView)
After clicking, the slot function will be triggered to obtain the current line information.