Important: Please read the Qt Code of Conduct -

One or Multiple QtTreeModel(s) in different Tabs?

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

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

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

  • 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

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion


    • 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();

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

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

  • Lifetime Qt Champion

    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 ?

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

  • Lifetime Qt Champion

    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.

  • thanks. i will try it :)