Hi there people. I'm pretty new at PyQt, or Qt at all,and was trying for some time to write few things with variable success. This time I feel really stuck, so I decide to ask for the kind help of someone, who actually knows what to do 
So here is my case:
I have a QT App (PyQt actually), which reads some data from a server based sql database, and updates a QStandardItemModel which is used by a QTreeView. I may be wrong, but I guessed the StandardModel is a better choise for my needs, because it also reads the messages from some server node across our network and so on. So my actual question is how can I refresh the QStandartItemModel, but retain the selected row also. I've made a QThread which emits a "refresh" signal every 5 second, and connected it to a custom pyqtSlot in my StandartItemModel which actually calls the __init__ procedure of the model and then reattaches the model to the QTreeView:
def __init__(self, parent, viewer, thread = None):
self.viewer = viewer
"""
Code...Code...Code
"""
if self.viewer:
self.viewer.setAlternatingRowColors(True)
self.viewer.setSortingEnabled(True)
self.viewer.setIndentation(0)
# This connects the QThread signal to the model's slot
if thread:
global refreshThread
self.connect(refreshThread, QtCore.SIGNAL("refresh()"), self.viewer, QtCore.SLOT('reset()'))
@QtCore.pyqtSlot()
def rebuild(self, thread = 0):
if state:
return # Halt if editing
self.__init__(None, self.viewer, 0)
self.viewer.setModel(self)
class fileList(QtGui.QStandardItemModel):
def __init__(self, parent, viewer, thread = None):
QtGui.QStandardItemModel.__init__(self,parent)
self.viewer = viewer
"""
Code...Code...Code
"""
if self.viewer:
self.viewer.setAlternatingRowColors(True)
self.viewer.setSortingEnabled(True)
self.viewer.setIndentation(0)
self.viewer.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
# This connects the QThread signal to the model's slot
if thread:
global refreshThread
self.connect(refreshThread, QtCore.SIGNAL("refresh()"), self.viewer, QtCore.SLOT('reset()'))
@QtCore.pyqtSlot()
def rebuild(self, thread = 0):
state = QtGui.QAbstractItemView.state(self.viewer)
if state:
return # Halt if editing
self.__init__(None, self.viewer, 0)
self.viewer.setModel(self)
To copy to clipboard, switch view to plain text mode
and the QThread Class:
def __init__(self, parent = None):
QtCore.
QThread.__init__
(self, parent
)
def run(self):
while True:
self.timeout = REFRESH if REFRESH != 0 else 0.1
if self.timeout == -1:
print "Refresh Halted"
break
self.emit(QtCore.SIGNAL('refresh()'))
time.sleep(self.timeout)
class Reloader(QtCore.QThread):
def __init__(self, parent = None):
QtCore.QThread.__init__(self, parent)
def run(self):
while True:
self.timeout = REFRESH if REFRESH != 0 else 0.1
if self.timeout == -1:
print "Refresh Halted"
break
self.emit(QtCore.SIGNAL('refresh()'))
time.sleep(self.timeout)
To copy to clipboard, switch view to plain text mode
So what is the right way to refresh the QStandardItemModel and retain the selection? I've tried using a QSelectionModel to which I added the selection and the set it as a selection model for the model viewer, but it messes up with my active selection until it refreshes, so I suspect it's totally wrong
Bookmarks