Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Best approach to use single QSettings instance in multiple widgets
Forum Updated to NodeBB v4.3 + New Features

Best approach to use single QSettings instance in multiple widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 873 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    npatil15
    wrote on last edited by npatil15
    #1

    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:

    • 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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved