我有一个小UI,列出给定路径的文本和python脚本文件,但是当我尝试使用新项目更新或替换列表时,我认为数据放在那里,但它们显示为空白。我认为这与没有告诉用户界面数据已经更新并且必须重新绘制UI有关?
这是我的代码:
import os
import platform
import shutil
import subprocess
import sys
import time
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.fileListerObject = fileListGenerator(self)
self.setObjectName(_fromUtf8("Form"))
self.resize(350, 300)
mainSizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)
mainSizePolicy.setHorizontalStretch(0)
mainSizePolicy.setVerticalStretch(0)
mainSizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
self.setSizePolicy(mainSizePolicy)
self.setSizePolicy(mainSizePolicy)
self.uiName = 'Tree test'
self.setWindowTitle(self.uiName)
self.windowElements()
def windowElements(self):
verticalLayout = QtGui.QVBoxLayout(self)
verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.inputLineEdit = QtGui.QLineEdit(self)
verticalLayout.addWidget(self.inputLineEdit)
buttonThing = QtGui.QPushButton(self)
buttonThing.setText('SET')
verticalLayout.addWidget(buttonThing)
self.connect(buttonThing, QtCore.SIGNAL("clicked()"), self.fileListerObject.fileLister)
self.treeWidget = QtGui.QTreeWidget(self)
self.treeWidget.setIndentation(5)
self.treeWidget.setAllColumnsShowFocus(True)
self.treeWidget.setObjectName(_fromUtf8("treeWidget"))
self.treeWidget.header().setSortIndicatorShown(True)
self.treeWidget.header().setSortIndicator(0, QtCore.Qt.AscendingOrder)
self.treeWidget.setSortingEnabled(True)
self.treeWidget.headerItem().setText(0, "file name")
self.treeWidget.headerItem().setText(1, "date")
self.treeWidget.headerItem().setText(2, "type")
self.__sortingEnabled = self.treeWidget.isSortingEnabled()
self.treeWidget.setSortingEnabled(False)
verticalLayout.addWidget(self.treeWidget)
buttonLaunchApp = QtGui.QPushButton(self)
buttonLaunchApp.setText('OPEN')
verticalLayout.addWidget(buttonLaunchApp)
self.show()
class fileListGenerator():
def __init__(self, parentWindow):
self.parentWindow = parentWindow
def fileLister(self):
directoryPath = self.parentWindow.inputLineEdit.text()
dirPath = directoryPath
fileList = os.listdir(dirPath)
projectFileList = []
if len(fileList) != 0:
for file in fileList:
if ( file.endswith('.txt') != False ) or ( file.endswith('.py') != False ):
projectFileList.append(file)
print projectFileList
itemID = 0
for item in projectFileList:
filePath = dirPath + '\\' + item
threePartSplit = item.rpartition(".")
extension = threePartSplit[2]
dateTimeStamp = time.ctime(os.path.getmtime(filePath))
QDate = QtCore.QDateTime.fromString (str(dateTimeStamp), 'ddd MMM dd HH:mm:ss yyyy')
QtGui.QTreeWidgetItem(self.parentWindow.treeWidget)
self.parentWindow.treeWidget.topLevelItem(itemID).setText(0, item)
self.parentWindow.treeWidget.topLevelItem(itemID).setData(1, QtCore.Qt.DisplayRole, QDate.toString('yyyy-MM-dd HH:mm:ss'))
self.parentWindow.treeWidget.topLevelItem(itemID).setText(2, extension)
itemID += 1
self.parentWindow.treeWidget.setSortingEnabled(self.parentWindow._Window__sortingEnabled)
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
我试过这个,我想出了一个使用QAbstractItem模型的答案?但据我所知,你只能使用qTreeVIEW,我正在使用widget版本。我对项目中的数据做的不多,所以我假设我不需要qTreeview,所以我想把它作为一个小部件。有这种设置的方法让UI正确更新数据吗?或者我需要完全重新格式化吗?
提前谢谢。
答案 0 :(得分:0)
老实说,我的代码并不完整。但是,如果您要将新项目添加到QTreeWidget
,请按以下步骤操作:
item = QTreeWidgetItem()
item.setText(0, "John") # first name
item.setText(1, "Doe") # last name
item.setText(2, "35") # age
self.treeWidget.addTopLevelItem(item)
答案 1 :(得分:0)
QTreeWidgets在添加新项目时更新/重绘。因此,添加一个项目,然后像这样删除它:
itemYouChanged.parent().removeChild(QTreeWidgetItem(itemYouChanged.parent()))