[solved]QAbstractListModel - update data in table view [linux]
-
I am just a beginner in Qt.
Perhaps instead of "model" I shoud reset "stockdata" (add to the line 6):@
def resetItems(self,stockdata):
self.beginResetModel()
self.__stockdata = stockdata
self.endResetModel()
@I took the example from this website:
http://www.nullege.com/codes/show/src@o@m@omg-0.3.1@omg@models@simplelistmodel.py/42/PyQt4.QtCore.QAbstractListModel.beginResetModel/python -
Hi,
You could use a QFileSystemWatcher so you get the information that the file has been modified and re-read it.
Hope it helps
-
I use QFileSystemWatcher like that:
@
file_check = QtCore.QFileSystemWatcher(['/home/user/file.txt'])
file_check.fileChanged.connect(resetItems)@"resetItems" looks like that:
@def resetItems(self,stockdata):
self.beginResetModel()
self.stockdata = stockdata
self.endResetModel()@it looks fine but when I run script I got the error:
TypeError: resetItems() takes exactly 2 arguments (1 given)
Could it be mistake in QFileSystemWatcher syntax?
thx for reply!
-
It looks like you are mixing several things here. Why did you change your class base type ?
-
@AttributeError: ‘QString’ object has no attribute ‘beginResetModel’@
means that you are trying to call beginResetModel on a QString class, which is wrong.
stockdata and __stockdata don't seem to represent the same thing, you should use more meaningful names for your variables to make your code easier to understand
-
It should be part of your subclass of QAbstractItemModel
-
what you have written means that this is wrong ?
@class StockListModel(QtCore.QAbstractListModel):
def init(self, stockdata = [], parent = None):
QtCore.QAbstractListModel.init(self, parent)
self.stockdata = stockdatadef getItems(self): return stockdata
#resetItems:
def resetItems(self,stockdata):
self.beginResetModel()
self.stockdata = stockdata
self.endResetModel()def rowCount(self, parent): return len(self.stockdata) def data(self, index, role): if role == QtCore.Qt.DisplayRole: row = index.row() value = self.stockdata[row] return value file_check = QtCore.QFileSystemWatcher(['/home/user/Desktop/plik.txt']) file_check.fileChanged.connect(resetItems)
if name == 'main':
app = QtGui.QApplication(sys.argv)
app.setStyle("plastique")tableView = QtGui.QTableView() tableView.show() a = os.popen("cat /home/user/Desktop/plik.txt") a = a.read() time_variable = QtCore.QString("%s"%a) model = StockListModel([time_variable]) tableView.setModel(model) sys.exit(app.exec_())@
and instead of QtCore.QAbstractListModel i should use QtCore.QAbstractItemModel ? or add aditional subclass?
-
Shouldn't stockdata be a list ? If so, your connection doesn't make sense
-
fileChanged gives you the path of the file that changed, not it's content, so basically you are replacing your list by a string.
Also, why are you using the model/view approach if you only have one entry ?
-
The signal will just tell you that the file has changed, it will not reload the content for you. You'll have to parse the file again and update stockdata with what you just read from it