0

I have a websocket and want to update my PyQt5-GUI every time there is a new packet coming in. There is a QAbstractTableModel to handle the conversion to the TableView. A QWidget wraps up the TableView and Model. In this QWidget I started a thread to listen to the data from the socket and as args I gave it the update-function, that should update the data and the table.

FieldTableModel is my QAbstractTableModel. packet_to_fnc receives data from the socket, throws it in a dict and passes it into update_table().

class FieldTableWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.field = Field(20)
        self.receiver = Thread(target=packet_to_fnc, args=(self.update_table,))
        self.receiver.start()
        self.table = QTableView()
        self.table.setModel(FieldTableModel(self.field))
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.table)
        self.setLayout(self.layout)

    def update_table(self, packet):
        self.field.update_field(packet)
        self.table.setModel(FieldTableModel(self.field))

Now when I start the GUI and data from the socket comes in, I get the following message in the console for every packet:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QHeaderView(0x16b0b6f5590), parent's thread is QThread(0x16b06c2c7a0), current thread is QThread(0x16b0b7b4610)

So my problem basically is that PyQt doesn't like the execution of update_table() in the Thread.
I was also thinking of using a @property-attribute in a class, but I guess it's the same outcome?

Is there a different way to trigger the Table-Update every time there is new socket data?

JulMai
  • 15
  • 3
  • this is to update a chart, but I assume, may work similar: https://stackoverflow.com/questions/71360222/best-way-to-chart-streamed-data-using-pyqtchart-or-pyqtgraph-with-pyqt5-on-pytho – Je Je Apr 07 '23 at 13:07
  • 1
    Use a QThread, with a custom signal, and connect it to the function that updates the model. Note that, unless every new packet would result in a *completely* new data layout, you should not create a new model every time, but instead update it either by inserting/removing rows and columns or by updating the data and emitting the `dataChanged` signal. – musicamante Apr 07 '23 at 18:22

0 Answers0