作为我学校项目的一部分,我一直在研究这段代码,而且我似乎无处可去。我的问题是当我尝试将文件中的文本添加到我的QlistWidget时,我的程序崩溃了。首先,我想知道这是否会将目录中的文件(movieDir.txt
中的每一行是一个目录)添加到QlistWidget,其次我甚至不确定我是否正在使用正确的小部件或者它应该是QlistView。另外我如何制作它,以便listWidget中当前选择的项目在文本框中显示细节(例如文件的大小)。
我也希望在启动时完成此操作,如果有人可以请告诉我addItems(self)
的哪个位置会非常感激。我不习惯使用课程,因为我们在学校里被忽视了这一点。
我在Windows 7上运行PyQt5
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'input.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(764, 500)
MainWindow.setMinimumSize(QtCore.QSize(764, 353))
MainWindow.setMaximumSize(QtCore.QSize(764, 500))
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
self.verticalLayout.setContentsMargins(11, 11, 11, 11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_2.setSpacing(6)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton_5 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_2.addWidget(self.pushButton_5)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setContentsMargins(11, 11, 11, 11)
self.verticalLayout_4.setSpacing(6)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_4.setSpacing(6)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.listView = QtWidgets.QListView(self.centralWidget)
self.listView.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)
self.listView.setViewMode(QtWidgets.QListView.ListMode)
self.listView.setObjectName("listView")
self.horizontalLayout_4.addWidget(self.listView)
self.textEdit = QtWidgets.QTextEdit(self.centralWidget)
self.textEdit.setObjectName("textEdit")
self.horizontalLayout_4.addWidget(self.textEdit)
self.verticalLayout_4.addLayout(self.horizontalLayout_4)
self.verticalLayout.addLayout(self.verticalLayout_4)
self.horizontalSlider = QtWidgets.QSlider(self.centralWidget)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.verticalLayout.addWidget(self.horizontalSlider)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setContentsMargins(11, 11, 11, 11)
self.verticalLayout_2.setSpacing(6)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout.setSpacing(6)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setContentsMargins(11, 11, 11, 11)
self.horizontalLayout_3.setSpacing(6)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.dial = QtWidgets.QDial(self.centralWidget)
self.dial.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.dial.setMouseTracking(True)
self.dial.setObjectName("dial")
self.horizontalLayout_3.addWidget(self.dial)
self.pushButton_4 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.horizontalLayout_3.addWidget(self.pushButton_4)
self.horizontalLayout.addLayout(self.horizontalLayout_3)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.verticalLayout.addLayout(self.verticalLayout_2)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 764, 21))
self.menuBar.setObjectName("menuBar")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton_5.clicked.connect(self.addItem)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_5.setText(_translate("MainWindow", "Show Movies"))
self.pushButton_3.setText(_translate("MainWindow", "Fast-Forward"))
self.pushButton_2.setText(_translate("MainWindow", "Pause/Play"))
self.pushButton.setText(_translate("MainWindow", "Rewind"))
self.pushButton_4.setText(_translate("MainWindow", "Full Screen"))
def addItem(self):
with open('movieDir.txt', 'r') as movieDir:
for row in movieDir:
listWidget.addItem(row)
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)
self.listWidget = QtWidgets.QListWidget(self.centralWidget)
self.listWidget.setObjectName("listWidget")
self.horizontalLayout_4.addWidget(self.listWidget)
with open('moviesDir.txt','r') as movieDir:
for movie in movieDir:
self.listWidget.addItem(movie)
这就是我所说的,但它只显示为文本,有没有办法在目录中显示文件而不是作为字符串的目录?
答案 1 :(得分:0)
#This is the example code for loading files and content inside the file to QtGui.QListWidget
#It is PyQt4, but you can try with PyQt5 with small changes.
#If your are not expecting this answer, sorry.
import sys, os
from PyQt4 import QtGui, QtCore
class Window (QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.verticalLayout = QtGui.QVBoxLayout (self)
self.verticalLayout.setObjectName ('verticalLayout')
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName('horizontalLayout')
self.listWidget = QtGui.QListWidget(self)
self.listWidget.setObjectName('listView')
self.listWidget.setAlternatingRowColors (True)
self.horizontalLayout.addWidget(self.listWidget)
self.verticalLayout1 = QtGui.QVBoxLayout()
self.verticalLayout1.setSpacing(10)
self.verticalLayout1.setObjectName('verticalLayout')
self.pushButton = QtGui.QPushButton(self)
self.pushButton.setObjectName('pushButton')
self.pushButton.setText('Load File Content')
self.pushButton_2 = QtGui.QPushButton(self)
self.pushButton_2.setObjectName('pushButton_2')
self.pushButton_2.setText('Load File')
self.verticalLayout1.addWidget(self.pushButton)
self.verticalLayout1.addWidget(self.pushButton_2)
spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
self.verticalLayout1.addItem (spacerItem)
self.horizontalLayout.addLayout(self.verticalLayout1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.pushButton.clicked.connect (self.loadFileContent)
self.pushButton_2.clicked.connect (self.loadFiles)
def loadFileContent (self) :
openFiles = QtGui.QFileDialog.getOpenFileName (self, 'Open File', 'c:/', 'txt (*.txt)')
if openFiles :
data = open (str(openFiles), 'r')
dataList = data.readlines ()
self.listWidget.clear ()
for eachLine in dataList :
if len(eachLine.strip())!=0 :
self.listWidget.addItem(eachLine.strip())
def loadFiles (self) :
getDirectory = QtGui.QFileDialog.getExistingDirectory(self, 'Browse', 'C:/')
if getDirectory :
fileList = os.listdir (str(getDirectory))
if fileList :
self.listWidget.clear ()
for eachFile in fileList :
self.listWidget.addItem (eachFile)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
#Thanks,
#Subin Gopi