0
我有一個列出給定路徑的文本和python腳本文件的小UI,但是當我嘗試更新或用新項目替換列表時,我認爲數據放置在那裏,但它們顯示爲空白。我認爲這與不告訴用戶界面數據已更新以及用戶界面是否需要重新繪製有關?如何更新/刷新qTreewidget?
這裏是我的代碼:
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,並使用小部件版本。我對項目中的數據做的並不多,所以我假設我不需要qTreeview,所以我想保留它作爲一個小部件。有沒有這樣的設置讓UI更新數據的方法?或者我需要徹底重新格式化?
預先感謝您。
嗨,非常感謝你的迴應。這似乎是有效的,但我不明白,爲什麼這種方法有效,而不是我的方法?當然,我所做的只是在Designer中製作用戶界面,將其轉換爲Python腳本,並試着弄清楚它是如何放置的。只說「topLevelItem(itemID).setText()」和「addTopLevelItem(item)」之間有什麼區別?我的意思是在打字時,我可能會明白爲什麼當我已經存在的物品時,我的工作不起作用,但即使當我使用clear()函數時,它仍然給我提供了相同的空白結果... – user3696118
@ user3696118如果您'重新更新現有的項目,'topLevelItem(itemID).setText()'應該工作。但要添加一個新項目,首先需要創建一個'QTreeWidgetItem',然後像上面所做的那樣將它添加到樹中。 – xyres
qtreewidget.clear()不清除小部件中的現有數據?我假設一切都被清除了,你可以把它看作是從零開始加載的。 – user3696118