mardi 6 décembre 2022

Add Widgets from files automatically in pyQT

I would like to add widgets to my QT application App.py more or less dynamically by serving the widgets through a folder structure like so

App.py
plugins
-> plugin_A
   -> main.py
- plugins_B
   -> main.py

Every main.py is representing a QT widget which is then shown in the main application.

+-------------------+
| PLUGIN A          |
| [Button]          |
| [Label]           |
+-------------------+
| PLUGIN B          |
| [Checkbox 1]      |
| [Checkbox 2]      |
| [Button]          |
+-------------------+
| ...               |

I thought about simply scanning the plugins folder, creating and saving the Widgets in a list and adding them to a layout in a loop.

Upside I could easily propagate down common functionality to the constructor, like a COM port or something like that.

Downside The approach feels naiv and it would feel much better if the Widgets would register themselves actively somehow, but I don't know if and why this way is maybe preferable.

The whole situation seems to be so common, that I wonder if there is a general pattern to start with, maybe QT already has some functionality for such cases?

Aucun commentaire:

Enregistrer un commentaire