Important: Please read the Qt Code of Conduct -

Need help on program structure

  • Hopefully I can explain this. My Qt app can potentially interact with 4 different types of devices, but only one type at a time. There can be up to 8 devices at a time (all the same type). There are radio buttons to select the type of device. When the user clicks "Start", a different function is called depending on the type of device selected, let's call them ProcessDeviceA, ProcessDeviceB, etc.

    There is a list (currently QList) of up to 8 devices of the selected type, lets call the list Devices. Devices needs to be a different type, of course depending on the device type the user selected. The Devices list must be accessed in ProcessDeviceA, ProcessDeviceB, etc. ProcessDeviceA must access Devices for its specific type of device, etc. There is really little in common between the different device types.

    At the module level, must I have 4 different lists DevicesA, DevicesB, etc. and just operate on whichever one is necessary in ProcessDeviceA, ProcessDeviceB, etc. Or can I use polymorphism somehow? This will ultimately be in an embedded environment so I need to conserve RAM.


  • Lifetime Qt Champion


    What about having an abstract base class and then make your devices from it ?

    Your ProcessDeviceXXX functions can receive e.g. QList<Devices> and when doing the processing checks whether the device is of the correct type and if not just do nothing.

    Hope it helps

  • That's a good idea, exactly the kind of thing I was looking for. Thanks.

  • SGaist, it seems the base class Device would be possibly be empty. In this case, should I use inheritance for this? My app will still have to check what mode the program is in to decide what functions to call. I'm not clear how inheritance can help me here, but it just seems like it could. I will keep thinking and reading.

  • Lifetime Qt Champion

    Indeed, an interface is also a possibility

Log in to reply