Questions about MVC

Hi,
I have some questions while reading the book “Create GUI Application with Python & Qt6: The hands-on guide to making apps with Python”.

The section “Create the Model” in Chapter 18 Introduction to MVC gives an example (Listing 108. further/mvc_5.py) to demonstrate how to make an instance of model be able to emit signal while it is updated.

What I don’t understand is the part which class DataModelSignals(QObject) needs be a subclass of QObject. Can’t we just create custom signal in the init of the class Datamodel(UserDict)?

If I remember correctly Signal() has to be wrap around with class type of QObject to work as QT Signal.

You can easily verify it but changing example code. I’m outside my home so can’t check myself :wink:

EDIT: QObject() deliver necessary API connect() and emit() for Signal() to work.

1 Like

That’s right @Michal_Plichta you can only create signals on subclasses of QObject.

1 Like

Hi, @Michal_Plichta and @martin,
Thanks for your reply.
If I understand correctly, we can only use Signal() in the subclass of QObject because the class provides methods for defined signals (like connect, emit). Then, in Listing 258. further/signals_custom.py (Chapter 35), we can use Signal() in the class MainWindow(QMainWindow) is because QMainWindow is a subclass of QObject. Therefore, the custom class MainWindow which is the subclass of QMainWindow also inherit those methods in QObject. However, the UserDict in the Listing 108. further/mvc_5.py is not a subclass of QObject, therefore we need to additionally define a class DataModelSignals(QObject) and make an instance in the class DataModel(UserDict) so that to use Signal().

Yes, this is correct!