Best approach to use single QSettings instance in multiple widgets

  • Hello Everyone,

    I'm working on a project where I have to do this following job: Consider bold text as a widget,

    • MainWidget: Create tab widget and add all these below widgets as a tab.
    • Import: Import JSON files and extract data from files.
    • Charts: Visualize data
    • Export: Export data to Excel


    • Import files
    • I have QSetings which has a list of keys to be extracted from those JSON files.

    This QSetting object is created in MainWidget and shared with all those widgets to tract the imported data and will be used in Charts and Export.

    For now, I have created a single object share to all those widgets. but I have found some solutions like

    • Singleton method
    • Using namespace
      Maybe there are more best approaches for this to have a single object/instance and share with all widgets.

    Please provide your suggestions.

    Thanks for reading up on this.

  • Lifetime Qt Champion

    Creating an object with the data and share that among the classes is the way to go for your use case.
    Sometimes signal and slot can be used - but else it's pretty fine to just share a data instance between objects.

    A Singleton is mostly a glorified global object so offer very little in terms of sharing versus
    plain global variables and such suffer the same disadvantages.

    You could use a unique_ptr or shared_ptr to control ownership but since all consumers of that data are in MainWindow, im not sure its
    not over designing.

  • Lifetime Qt Champion


    QSettings is a pretty inexpensive class that's usually used "on site" as each object would handle its own settings independently. If you have the need for a shared settings object, you would usually have a wrapper class that encapsulate it and that you would share as @mrjj suggested.

Log in to reply