Get Selected Files' Filenames From Pyqt5 Qlistview In Python
I am having trouble retrieving the filenames from selected QListview, I want the get the names of the files I selected from my list so that I can open the files by calling the file
Solution 1:
First do not modify the code provided by Qt Designer as indicated by the docs, instead create a class that inherits the appropriate widget and use the previous class to fill it. Going to the problem, you must use the function selectedIndexes() that return the QModelIndex to get the name through filePath of the model.
from PyQt5 import QtCore, QtGui, QtWidgets
classUi_MainWindow(object):
defsetupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(450, 550)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.listview = QtWidgets.QListView(self.centralwidget)
self.listview.setGeometry(QtCore.QRect(10,10,400,300))
self.listview.setObjectName("listview")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.pushButton.setGeometry(QtCore.QRect(300,380,100,20))
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
defretranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "App"))
self.pushButton.setText(_translate("MainWindow", "Retrieve data"))
classMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def__init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.btn_clk)
path = r'\\desktop\file'
self.fileModel = QtWidgets.QFileSystemModel(self.centralwidget)
self.fileModel.setRootPath(path)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.listview.setModel(self.fileModel)
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
@QtCore.pyqtSlot()defbtn_clk(self):
for ix in self.listview.selectedIndexes():
print(self.fileModel.filePath(ix), ix.data())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
UPDATE:
classUi_MainWindow(object):
defsetupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(450, 550)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.listview = QtWidgets.QListView(self.centralwidget)
self.listview.setGeometry(QtCore.QRect(10,10,400,300))
self.listview.setObjectName("listview")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.pushButton.setGeometry(QtCore.QRect(300,380,100,20))
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
defretranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "App"))
self.pushButton.setText(_translate("MainWindow", "Retrieve data"))
classUi_SecondWindow(object):
defsetupUi(self,SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(600, 400)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
SecondWindow.setCentralWidget(self.centralwidget)
self.tableView = QtWidgets.QTableView(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(10,10,580,370))
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
defretranslateUi(self,SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "Table"))
classSecondWindow(QtWidgets.QMainWindow, Ui_SecondWindow):
def__init__(self, parent=None):
super(SecondWindow, self).__init__(parent)
self.setupUi(self)
classMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def__init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.btn_clk)
path = r'\\desktop\file'
self.fileModel = QtWidgets.QFileSystemModel(self.centralwidget)
self.fileModel.setRootPath(path)
self.fileModel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.listview.setModel(self.fileModel)
self.listview.setRootIndex(self.fileModel.index(path))
self.listview.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.sw = SecondWindow()
@QtCore.pyqtSlot()defbtn_clk(self):
for ix in self.listview.selectedIndexes():
filename = self.fileModel.filePath(ix)
df = pd.read_csv(filename)
self.model = PandasModel(df)
self.sw.tableView.setModel(self.model)
self.sw.show()
returnif __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Post a Comment for "Get Selected Files' Filenames From Pyqt5 Qlistview In Python"