Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Stuck trying to understand model/view in Python
Forum Updated to NodeBB v4.3 + New Features

Stuck trying to understand model/view in Python

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phill
    wrote on 18 Jul 2017, 11:30 last edited by
    #1

    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()
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 Jul 2017, 22:09 last edited by
      #2

      Hi,

      Basically it's the same way as C++ as described here.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 19 Jul 2017, 13:26
      0
      • S SGaist
        18 Jul 2017, 22:09

        Hi,

        Basically it's the same way as C++ as described here.

        P Offline
        P Offline
        Phill
        wrote on 19 Jul 2017, 13:26 last edited by
        #3

        @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?

        K 1 Reply Last reply 19 Jul 2017, 18:55
        0
        • P Phill
          19 Jul 2017, 13:26

          @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?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 19 Jul 2017, 18:55 last edited by
          #4

          @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.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1

          1/4

          18 Jul 2017, 11:30

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved