PyQt5 – Copy QObject child

Question:

I have a pyqt5 app with an MVC structure similar to the one described here.
When the model is edited, a pyqtSignal is emitted, after which the controller will update the view (and, of course, the other way around). To enable the use of pyqtSignals, the model class inherits from the QtCore.QObject class.

I now want to duplicate the current "window" – creating a copy of the model, and coupling it to a newly created view and controller. However, the fact that the model inherits from QtCore.QObject makes it so I cannot copy it (TypeError: cannot pickle 'Model' object – QObjects cannot be copied by design).

What would be the best way to approach this problem?

Asked By: Wouter

||

Answers:

Based on @ekhumoro suggestion, I ended up creating a separate data-class. The ‘model’-class has an instance of this data-class and makes it so I can easily create copies.

In most cases it might be easier to try to migrate all model-changed-signals to the controller instead, though.

Answered By: Wouter