Get selected files' filenames from PYQT5 QList

2019-08-27 02:42发布

问题:

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 filenames I retrieved. However, this doesn't work...

Also, how do I set a multi select mode and retrieve all the files' filenames selected??

from PyQt5 import QtCore, QtGui, QtWidgets
import PandasModel
import pandas as pd


class Ui_MainWindow(object):
    def setupUi(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")

        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.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)


    def retranslateUi(self, MainWindow):
         _translate = QtCore.QCoreApplication.translate
         MainWindow.setWindowTitle(_translate("MainWindow", "App"))

         self.pushButton.setText(_translate("MainWindow", "Retrieve data"))

         self.pushButton.clicked.connect(self.btn_clk)

         MainWindow.show()

    def get_name(self,index):
        return index.data.toString

    def btn_clk(self,selected):

        filename =self.get_name(self.listview.clicked)
        print(filename)

        SecondWindow = QtWidgets.QMainWindow(MainWindow)
        self.SW = Ui_SecondWindow()
        self.SW.setupUi(SecondWindow)

        SecondWindow.show()
        df = pd.read_csv(filename)
        model = PandasModel(df)
        self.SW.tableView.setModel(model)

class Ui_SecondWindow(object):
    def setupUi(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)

    def retranslateUi(self,SecondWindow):
        _translate = QtCore.QCoreApplication.translate
        SecondWindow.setWindowTitle(_translate("SecondWindow", "Table"))

        SecondWindow.show()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

回答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

class Ui_MainWindow(object):
    def setupUi(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)

    def retranslateUi(self, MainWindow):
         _translate = QtCore.QCoreApplication.translate
         MainWindow.setWindowTitle(_translate("MainWindow", "App"))
         self.pushButton.setText(_translate("MainWindow", "Retrieve data"))

class MainWindow(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()
    def btn_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:

class Ui_MainWindow(object):
    def setupUi(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)

    def retranslateUi(self, MainWindow):
         _translate = QtCore.QCoreApplication.translate
         MainWindow.setWindowTitle(_translate("MainWindow", "App"))
         self.pushButton.setText(_translate("MainWindow", "Retrieve data"))


class Ui_SecondWindow(object):
    def setupUi(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)

    def retranslateUi(self,SecondWindow):
        _translate = QtCore.QCoreApplication.translate
        SecondWindow.setWindowTitle(_translate("SecondWindow", "Table"))

class SecondWindow(QtWidgets.QMainWindow, Ui_SecondWindow):
    def __init__(self, parent=None):
        super(SecondWindow, self).__init__(parent)
        self.setupUi(self)

class MainWindow(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()
    def btn_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()
            return

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())