How to update/refresh qTreewidget?
<p>I have this little UI that lists text and python script files for a given path, but when I try to UPDATE or REPLACE the list with new items, I think the data is placed there, but they appear blank. I assume it has something to do with not telling the UI that data has been updated and that the UI has to be redrawn?</p>

<p>Here is my code:</p>

<pre><code>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()
</code></pre>

<p>I tried looking this up and I came up with an answer of using a QAbstractItem model? But from what I understand you can only use those with a qTreeVIEW, and I'm using the widget version. I'm not doing much with the data in the item so I assume I wouldn't be needing a qTreeview, so I'd like to keep it as a widget. Is there a way with this setup to have the UI update the data correctly? Or do I need to completely reformat this?</p>

<p>Thank you in advance.</p>

<p>Honestly, I don't quite get your code. But if you want to add new items to a <code>QTreeWidget</code>, here's how you do it:</p>

<pre><code>item = QTreeWidgetItem()
item.setText(0, "John") # first name
item.setText(1, "Doe") # last name
item.setText(2, "35") # age

self.treeWidget.addTopLevelItem(item)
</code></pre>

python python-2.7 qt pyqt pyqt4