0

So I'm currently working on a GUI in PyQt5 and I'm creating a window based off of a yaml settings file that contains information about what widgets should be contained within the window and some of their attributes. I have implemented it using just a single class but am thinking of refactoring it to use the factory design pattern but the issue is that certain widgets are composed of other widgets (tabs, stacked widgets) and I make a recursive call when creating the tabs and stacked widgets.

So this is a minimal example of what I have:

class WindowCreator():
  def createWidget(self, parent, settings):
    if settings.type == 'tab':
      self.createTab(self, parent, settings)  
    if esttings.type == 'aTypeOfWidget':
      self.createATypeOfWidget(self, parent, settings)
    if settings.type == 'anotherTypeOfWidget':
      self.createAnotherTypeOfWidget(self, parent, settings)

  def createTab(self, parent, settings):
    # create tab elements
    for item in settings.items:
       self.createWidget(self, parent, settings)    

What I thought of doing was creating a simple abstract class that would define a create method and then create subclasses of that for each of the components.

class Base():
  def create(parent, settings): ...

class Tab(Base):
  def create(parent, settings):
    #implementation that can call create for other derived classes
    CheckBox.create(parent, settings)
    #this is where I'm stuck

class CheckBox(Base):
  def create(parent, settings):
     #implementation that will only return a QCheckBox

Is there a way to do this within the factory design pattern? Or is there a better pattern that will keep the code cleaner?

5
  • What's the point of this? Qt already provides a UI definition file format for use with Qt Designer, and PyQt provides the pyuic tool and uic module for working with them. There's really nothing to be gained from attempting to reinvent this wheel. Commented Jul 5, 2023 at 10:42
  • This allows the yaml to be altered and the window changes to match without having to manually update the UI. Commented Jul 5, 2023 at 12:02
  • That's exactly what Qt Designer does. Commented Jul 5, 2023 at 12:36
  • QtDesigner isn't an option. I need end users to be able to edit the yaml, it be as simple as possible and generate a window with the required elements. Having them edit .UI or worse actually using QtDesigner isn't an option. Trust me I wouldn't have opted for this approach if QtDesigner/UI was an option. Commented Jul 5, 2023 at 13:45
  • As I pointed out in my first comment, there's an existing tool-chain for converting Qt Designer files to PyQt modules. UI files are xml. There are dozens of tools available for converting xml to and from yaml (and many other formats). The UI file format is somewhat modular, so it isn't necessary to implement the whole specification. Commented Jul 5, 2023 at 17:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.