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. One or Multiple QtTreeModel(s) in different Tabs?

One or Multiple QtTreeModel(s) in different Tabs?

Scheduled Pinned Locked Moved Solved General and Desktop
treemodeltabsmodel-viewmvc
13 Posts 4 Posters 4.4k Views
  • 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.
  • O Offline
    O Offline
    Opa114
    wrote on 9 Aug 2017, 21:48 last edited by
    #1

    Hi there,

    i know my question is a question in general programming design, but because i'm using Qt releated stuff i'm asking here.

    So in my application i have a TableView with 5 Tabs. In each tab a TreeModel will be shown if i select the tab. Every TableView looks identical, but has different data which came from different sources.

    So what's the best and correct way? Using one TreeModel-Instance which will be shown on each tab an populated with the different data? Or having a TreeModel per tab which means 5 TreeModels in my case.

    If first one is the right or a good solution, how can i update or reset the TreeModel to the new data?

    E 1 Reply Last reply 9 Aug 2017, 22:04
    0
    • O Opa114
      9 Aug 2017, 21:48

      Hi there,

      i know my question is a question in general programming design, but because i'm using Qt releated stuff i'm asking here.

      So in my application i have a TableView with 5 Tabs. In each tab a TreeModel will be shown if i select the tab. Every TableView looks identical, but has different data which came from different sources.

      So what's the best and correct way? Using one TreeModel-Instance which will be shown on each tab an populated with the different data? Or having a TreeModel per tab which means 5 TreeModels in my case.

      If first one is the right or a good solution, how can i update or reset the TreeModel to the new data?

      E Offline
      E Offline
      Eeli K
      wrote on 9 Aug 2017, 22:04 last edited by
      #2

      @Opa114 I don't see anything wrong with either of them. Maybe you could find out the resource consumption of each solution. Five takes more memory than one but loading new data takes probably more time than switching between models. If the end user will change tabs often, 1 second for loading and showing new data is too much IMO.

      1 Reply Last reply
      1
      • O Offline
        O Offline
        Opa114
        wrote on 10 Aug 2017, 08:41 last edited by
        #3

        thanks for your opinion. "Wrong" is maybe the wrong word :) I think optimal or "standard" solution describes it better.

        In my TreeModels are not so much data entries, i think per TreeModel are at maximum 100 data entries, so there are not so many data entries which must be processed. Therefor i think memory management is not so important as the loading time for the user.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 10 Aug 2017, 09:03 last edited by
          #4

          QStandardItemModel can handle trees nicely. If the data in the views does not depend on one another separate the models to make them more maintainable

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • O Offline
            O Offline
            Opa114
            wrote on 11 Aug 2017, 17:31 last edited by
            #5

            so is this procedure correct?

            in my main-class i have a method which checks which tab was selected and then it decides which function have to be called. So if tab A is clicked a function like loadDataForA() ist called. inside this function i create a new Object of my custom TreeModel like TreeModel *model = new TreeModel(...) and then i set the data to the model and the model to the view. Which works.

            The same thing i have done for the other tab functions, but is this a good way?? so everytime i click on tab A a new TreeModel-Object will be created which consumes more and more memory or not? But the data has to be loaded each time from the source if i click the tab.

            Or should i create at start one TreeModel object like above, then call my functions and instead of creating there everytime a new model and clear the whole data of the model and then set the new data?

            And what about calling delete OBJECT in QT? In pure C++ i have to call delete OBJECT if i create it with new and if i don't need the Object anymore.

            Maybe you can tell me more about this fundamental design thing. Thanks a lot!

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 12 Aug 2017, 08:06 last edited by mrjj 8 Dec 2017, 08:07
              #6

              @Opa114 said in One or Multiple QtTreeModel(s) in different Tabs?:

              b A a new TreeModel-Object will be created

              Stop :)
              No need for that at all. Place the models as members of the class
              ( in the one with all the tabs for example)
              and new the models once in constructor of the class.

              1 Reply Last reply
              0
              • O Offline
                O Offline
                Opa114
                wrote on 12 Aug 2017, 09:48 last edited by
                #7

                yeah that's one option i have. But later i maybe have a dynamic count of tabs. Actual there are a fixed count, let's say 3 Tabs, but later there could be in a dynamic way more tabs, maybe 5 or maybe 10.

                But then i have to write a function inside my TreeModel class which clear's / reset all the data, so that i can populate the model with the new data when selecting a new tab or i'm wrong? How can i dlete the whole data entries in the model?

                M 1 Reply Last reply 12 Aug 2017, 12:26
                0
                • O Opa114
                  12 Aug 2017, 09:48

                  yeah that's one option i have. But later i maybe have a dynamic count of tabs. Actual there are a fixed count, let's say 3 Tabs, but later there could be in a dynamic way more tabs, maybe 5 or maybe 10.

                  But then i have to write a function inside my TreeModel class which clear's / reset all the data, so that i can populate the model with the new data when selecting a new tab or i'm wrong? How can i dlete the whole data entries in the model?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 12 Aug 2017, 12:26 last edited by mrjj 8 Dec 2017, 12:27
                  #8

                  @Opa114

                  • data which came from different sources.

                  Lets look at the case this way.
                  Each tab uses a model as data is pr tab.
                  So when you create a new tab, you also create its model.
                  You simply use a list of models.
                  That would be a much cleaner way than trying to clear the model.
                  There is no reason to reuse a model.
                  Simply new one for each new tab.

                  in .h for the class with tabs
                  QList<TreeModel *> tabModels;

                  ...
                  void AddNewTab( ) {
                  // make new tab..

                  TreeModel *m= new TreeModel();
                  tabModels.append(m);

                  // also give it to view in tab
                  }

                  This means that the tabs model , is in same index as the tab is.
                  this only is true if u cannot rearrange the tabs or remove/close them.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    Opa114
                    wrote on 12 Aug 2017, 13:37 last edited by
                    #9

                    i understand - yes that is a nice solution. Thanks.

                    But what if i use a TreeWidget with some nodes as menu entries instead of Tabs for navigation? Okay i could use the same technique ad you described above but is your solution so good with view to memory management if i create for each tab a TreeModel? Maybe some tabs did not contain data or as i mention before the count of the tabs could be from 1 to X and x maybe could be 100, so i have 100 tabs. But i think more than 30 it wouldn't.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 12 Aug 2017, 15:22 last edited by
                      #10

                      Hi
                      If you use a menu. what would happen if you click the menu ?

                      If you need a pool of data available to different view at different times then
                      make a small system to handle it, independently of what widgets used to show it.

                      If you use a map
                      std::map<QString, TreeModel *>
                      You get a list where you can ask for a model by name.

                      you can define a small interface for control

                      TreeModel * getModel(QString name)
                      // search for the model in the map
                      // if not found, it loads it.

                      TreeModel * RemoveModel(QString name)
                      // if you need to deallocate sometimes.

                      Then in mainwindow, you use those functions to get a model.
                      If you make a tab, part of constructing that you call
                      getModel("set 555");
                      to do what ever u need access to them to do.

                      There could be many others ways, but it really depends on what the program should do
                      and how.

                      Regarding memory. Depending on what platform app runs on, 30 x some models and
                      the data might not be massive.

                      What is the actual data. how much of it ?

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        Opa114
                        wrote on 12 Aug 2017, 22:09 last edited by
                        #11

                        when i use a menu or a custom menu (a TreeWidget) then the data will be read from the specific source and loaded into my custom TreeModel and then the model will be set to the TreeView. The data will be read from the source everytime when i click on the menu entry.

                        I don't read the data once and hold them. It could be that the source is changing the data while my application is running and therefore the data must be read again when clicking on the menu entry.

                        It's not so much data i'm reading and using. I think pro model (tab) which are maybe around 30 there are 50-70 entries. Entries are X.509-Certificates with some general information on it which are displayed, but the Certificates are stored in my model for accessing them. So this is maybe not a problem for the Memory.

                        M 1 Reply Last reply 13 Aug 2017, 08:11
                        0
                        • O Opa114
                          12 Aug 2017, 22:09

                          when i use a menu or a custom menu (a TreeWidget) then the data will be read from the specific source and loaded into my custom TreeModel and then the model will be set to the TreeView. The data will be read from the source everytime when i click on the menu entry.

                          I don't read the data once and hold them. It could be that the source is changing the data while my application is running and therefore the data must be read again when clicking on the menu entry.

                          It's not so much data i'm reading and using. I think pro model (tab) which are maybe around 30 there are 50-70 entries. Entries are X.509-Certificates with some general information on it which are displayed, but the Certificates are stored in my model for accessing them. So this is maybe not a problem for the Memory.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 13 Aug 2017, 08:11 last edited by
                          #12

                          @Opa114
                          Ok, it doesn't sound very heavy so should not be a memory issues if you run on
                          a desktop class pc/not ultra tiny board.

                          Since you must reload all data each time, im not sure how is saved reusing the model but
                          you can do that with QStandardItemModel ::clear()
                          Alternativ, just delete and create new model with new/updated data.

                          1 Reply Last reply
                          0
                          • O Offline
                            O Offline
                            Opa114
                            wrote on 14 Aug 2017, 15:49 last edited by
                            #13

                            thanks. i will try it :)

                            1 Reply Last reply
                            0

                            10/13

                            12 Aug 2017, 15:22

                            • Login

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