我试图用Python构建简单的FTP客户端,对于GUI,我使用PyQt5。问题是当我运行程序QTreeView时不显示目录时,它只是空白表面。我该如何编码以显示文件夹以及如何与下面的listView链接?在Google上搜索了我,但无法解决。
我得到的这个窗口
我想要这样的东西
代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(852, 637)
MainWindow.setAnimated(True)
MainWindow.setDocumentMode(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.connectButton = QtWidgets.QPushButton(self.centralwidget)
self.connectButton.setGeometry(QtCore.QRect(710, 20, 111, 21))
self.connectButton.setObjectName("connectButton")
self.usernameEdit = QtWidgets.QLineEdit(self.centralwidget)
self.usernameEdit.setGeometry(QtCore.QRect(180, 20, 161, 20))
self.usernameEdit.setObjectName("usernameEdit")
self.passEdit = QtWidgets.QLineEdit(self.centralwidget)
self.passEdit.setGeometry(QtCore.QRect(350, 20, 161, 20))
self.passEdit.setObjectName("passEdit")
self.portEdit = QtWidgets.QLineEdit(self.centralwidget)
self.portEdit.setGeometry(QtCore.QRect(520, 20, 161, 20))
self.portEdit.setReadOnly(False)
self.portEdit.setObjectName("portEdit")
self.hostEdit = QtWidgets.QLineEdit(self.centralwidget)
self.hostEdit.setGeometry(QtCore.QRect(10, 20, 161, 20))
self.hostEdit.setText("")
self.hostEdit.setObjectName("hostEdit")
self.treeView = QtWidgets.QTreeView(self.centralwidget)
self.treeView.setGeometry(QtCore.QRect(10, 60, 401, 281))
self.treeView.setObjectName("treeView")
self.treeView_2 = QtWidgets.QTreeView(self.centralwidget)
self.treeView_2.setGeometry(QtCore.QRect(420, 60, 401, 281))
self.treeView_2.setObjectName("treeView_2")
self.listView = QtWidgets.QListView(self.centralwidget)
self.listView.setGeometry(QtCore.QRect(10, 350, 401, 192))
self.listView.setObjectName("listView")
self.listView_2 = QtWidgets.QListView(self.centralwidget)
self.listView_2.setEnabled(True)
self.listView_2.setGeometry(QtCore.QRect(420, 350, 401, 192))
self.listView_2.setObjectName("listView_2")
self.uploadButton = QtWidgets.QPushButton(self.centralwidget)
self.uploadButton.setGeometry(QtCore.QRect(10, 550, 401, 41))
self.uploadButton.setObjectName("uploadButton")
self.downloadButton = QtWidgets.QPushButton(self.centralwidget)
self.downloadButton.setGeometry(QtCore.QRect(420, 550, 401, 41))
self.downloadButton.setObjectName("downloadButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 852, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "My FTP Client"))
self.connectButton.setText(_translate("MainWindow", "Quickconnect"))
self.usernameEdit.setPlaceholderText(_translate("MainWindow", "Username"))
self.passEdit.setPlaceholderText(_translate("MainWindow", "Password"))
self.portEdit.setPlaceholderText(_translate("MainWindow", "Port"))
self.hostEdit.setPlaceholderText(_translate("MainWindow", "Host"))
self.uploadButton.setText(_translate("MainWindow", "Upload"))
self.downloadButton.setText(_translate("MainWindow", "Download"))
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_())
答案 0 :(得分:1)
如果QTreeView
不显示要显示的内容,您希望如何显示它?QTreeView
所做的是显示某些模型的内容,在这种情况下,您应该使用QFileSystemModel
。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
...
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
model = QtWidgets.QFileSystemModel(self)
model.setRootPath(QtCore.QDir.rootPath())
self.treeView.setModel(model)
self.treeView.expandAll()
self.treeView.setRootIndex(model.index(QtCore.QDir.rootPath()))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())