我在从选定的QListview检索文件名时遇到麻烦,我想要从列表中选择文件的名称,以便可以通过调用检索到的文件名来打开文件。但是,这不起作用...
此外,如何设置多选模式并检索所有文件的文件名?
public class EnumCollection<T> : IEnumerable<T>
{
}
EnumCollection<T> numQuery = numbers.Where(num => num % 2 == 0).OrderBy(n => n);
答案 0 :(得分:0)
首先不要按照文档所述修改Qt Designer提供的代码,而是创建一个继承适当小部件的类,并使用先前的类来填充它。要解决该问题,必须使用返回QModelIndex的函数selectedIndexes()来通过模型的filePath获取名称。
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_())
更新:
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_())