Hi all,
I just ran int this issue where I'm trying to fill a model via
QRunnable, but when I do so, it seems to add extra rows for some
mysterious reasons.
If I run the respective process in in the main thread I get the expected
results.
Can somebody please help?
Test code attached.
Cheers,
frank
from PySide import QtCore, QtGui
class AnalyseTask(QtCore.QRunnable):
def __init__(self, model):
super(AnalyseTask, self).__init__()
self.model = model
def run(self):
process(self.model)
def process(path, model):
path = '/tmp/exampleTree2'
sub_dirs_dict = {}
for cur_root, cur_dirs, cur_files in os.walk(path):
if not model.rowCount():
# first iteration - add item to model
cur_root_item = QtGui.QStandardItem(cur_root)
model.appendRow(cur_root_item)
else:
# subsequent iterations need to look up the item
# from previous iterations through sub dirs
try:
for item in sub_dirs_dict[os.path.dirname(cur_root)]:
if item.data(QtCore.Qt.DisplayRole) == cur_root:
cur_root_item = item
# remove the item from the list
sub_dirs_dict[os.path.dirname(cur_root)].remove(item)
if not len(sub_dirs_dict):
# if no keys are left delete the dictionary
del sub_dirs_dict
except KeyError:
# reached leaf
pass
#### SUB DIRS ##########################################
item_counter = 0
sub_dirs_dict[cur_root] = []
for d in cur_dirs:
abs_path = os.path.join(cur_root, d)
dir_item = QtGui.QStandardItem(abs_path)
cur_root_item.setChild(item_counter, dir_item)
sub_dirs_dict[cur_root].append(dir_item)
item_counter += 1
##### FILES ##########################################
for f in cur_files:
file_item = QtGui.QStandardItem(os.path.join(cur_root, f))
cur_root_item.setChild(item_counter, file_item)
item_counter += 1
if __name__ == '__main__':
import sys
import os
app = QtGui.QApplication([])
view = QtGui.QTreeView()
model = QtGui.QStandardItemModel()
proxy = QtGui.QSortFilterProxyModel()
proxy.setSourceModel(model)
view.setModel(proxy)
# If I run process() like this the model gets extra rows:
task = AnalyseTask(model)
QtCore.QThreadPool.globalInstance().start(task)
# If I just run it in the main thread it wall works as expected:
#process(model)
view.show()
view.expandAll()
view.resize(800,600)
view.raise_()
sys.exit(app.exec_())_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside