Stuck trying to understand model/view in Python
-
wrote on 18 Jul 2017, 11:30 last edited by
I'm trying to learn model/view programming in python. There is one key part that I feel I'm stuck on in the minimal code below, the Device class communicates with a physical device over a network connection. At the moment, I'm trying to monitor if the device is recording or not. How should I notify the model of a change in any of the devices?
import sys from PyQt5 import QtCore, QtWidgets class Device: def __init__(self, name, ip_address): self.name = name self.ip_address = ip_address self.recording = False self.connected = False def communiction(self): # Method to communicate with and obtain recording status pass class DeviceModel(QtCore.QAbstractTableModel): def __init__(self, devices): super().__init__() self.devices = devices def columnCount(self, index): return 3 def rowCount(self, index): return len(self.devices) def headerData(self, section, orientation, role): if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: if section == 0: return 'Device' if section == 1: return 'Recording' if section == 2: return 'Connected' return None def data(self, index, role): if not index.isValid(): return if role == QtCore.Qt.DisplayRole: device = self.devices[index.row()] if index.column() == 0: return device.name if index.column() == 1: return device.recording if index.column() == 2: return device.connected else: return None class Main(QtWidgets.QTableView): def __init__(self): super().__init__() self.devices = [Device('Device 1', 'x.x.x.x'), Device('Device 2', 'x.x.x.x'), Device('Device 3', 'x.x.x.x')] self.device_model = DeviceModel(self.devices) self.setModel(self.device_model) def main(): app = QtWidgets.QApplication(sys.argv) form = Main() form.show() app.exec() if __name__ == '__main__': main()
-
Hi,
Basically it's the same way as C++ as described here.
-
wrote on 19 Jul 2017, 13:26 last edited by
@SGaist Thanks for your reply, but I can only find information about changing the data through the model on that page.
The page does state:
"The data itself does not have to be stored in the model; it can be held in a data structure or repository provided by a separate class, a file, a database, or some other application component."But does not allude to a way for the data structure to let the model know that it has changed. For example if "Device 1" communicates that recording has stopped how would I inform the model that the recording attribute has changed?
-
@SGaist Thanks for your reply, but I can only find information about changing the data through the model on that page.
The page does state:
"The data itself does not have to be stored in the model; it can be held in a data structure or repository provided by a separate class, a file, a database, or some other application component."But does not allude to a way for the data structure to let the model know that it has changed. For example if "Device 1" communicates that recording has stopped how would I inform the model that the recording attribute has changed?
@Phill said in Stuck trying to understand model/view in Python:
But does not allude to a way for the data structure to let the model know that it has changed. For example if "Device 1" communicates that recording has stopped how would I inform the model that the recording attribute has changed?
That is because it's entirely up to you. The model is just a interface (a facade) for the data. You must ensure that it emits the appropriate signals so it notifies the view, and if your model's data can be edited from the view, then it must also implement
QAbstractItemModel::setData
and friends, but there are no real requirements on how the model will bookkeep the dataset (if at all).One possibility is that your device is represented by a
QObject
and emits signals which are connected to slots in your model.
1/4