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. Need help to create simple model for QTreeModelView
Forum Updated to NodeBB v4.3 + New Features

Need help to create simple model for QTreeModelView

Scheduled Pinned Locked Moved Solved General and Desktop
37 Posts 4 Posters 3.7k 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.
  • Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by Please_Help_me_D
    #1

    Hi,

    I think I understood now how QAbstractItemModel work, of course I have already run few examples and now I'm trying to write my simple model with my items.
    I can launch it but I can see only header of the column (like on the picture) but there also should be one item. So the problem is that I don't see my item.
    I upload the project here
    Please take a look at it.
    0684a00b-5cf8-4f14-8486-e0db71cec4d6-image.png

    JonBJ 1 Reply Last reply
    0
    • Please_Help_me_DP Please_Help_me_D

      @mrjj I would be if not my birthday today :D

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #24

      @Please_Help_me_D
      :)
      Ok fair enough but it's also a bit hardcore to fiddle with QAbstractItemModel on
      such a special day :)

      alt text

      Please_Help_me_DP 2 Replies Last reply
      0
      • Please_Help_me_DP Please_Help_me_D

        Hi,

        I think I understood now how QAbstractItemModel work, of course I have already run few examples and now I'm trying to write my simple model with my items.
        I can launch it but I can see only header of the column (like on the picture) but there also should be one item. So the problem is that I don't see my item.
        I upload the project here
        Please take a look at it.
        0684a00b-5cf8-4f14-8486-e0db71cec4d6-image.png

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Please_Help_me_D

        Need help to create simple model for QTreeModelView

        Did you look at the Simple Tree Model Example?

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

          Hi
          Ran your code.
          You dont have any items ?
          I dont see you add anything to the list and it also reports zero

          alt text

          Ah you do add one here

          
          void MyModel::fetchRootDirectory()
          {
              _items.push_back(new MyItem("first_item"));
          }
          

          so the rowCount code seems not correct.

          1 Reply Last reply
          0
          • JonBJ JonB

            @Please_Help_me_D

            Need help to create simple model for QTreeModelView

            Did you look at the Simple Tree Model Example?

            Please_Help_me_DP Offline
            Please_Help_me_DP Offline
            Please_Help_me_D
            wrote on last edited by
            #4

            @JonB thank you for reply
            Yes I saw this. It is opened in my editor but I still don't understand why I cannot run mine
            @mrjj yes, I add item in fetchRootDirectory

            mrjjM 1 Reply Last reply
            0
            • Please_Help_me_DP Please_Help_me_D

              @JonB thank you for reply
              Yes I saw this. It is opened in my editor but I still don't understand why I cannot run mine
              @mrjj yes, I add item in fetchRootDirectory

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Please_Help_me_D
              But when view ask how many items you got via rowCount, you return zero.
              as in No rows and that is what you see.

              Please_Help_me_DP 1 Reply Last reply
              1
              • mrjjM mrjj

                @Please_Help_me_D
                But when view ask how many items you got via rowCount, you return zero.
                as in No rows and that is what you see.

                Please_Help_me_DP Offline
                Please_Help_me_DP Offline
                Please_Help_me_D
                wrote on last edited by
                #6

                @mrjj Why do I return zero?
                Are the comments below are true?

                int MyModel::rowCount(const QModelIndex &parent) const
                {
                    if (!parent.isValid()) { // if this is  a root (there is no parent)
                        return _items.size(); // this returns 1 and the application goes here
                    }
                    const MyItem* parentItem = static_cast<const MyItem*>(parent.internalPointer()); // 
                
                    return parentItem->children.size(); //returns zero but the application never reach there
                }
                
                Please_Help_me_DP 1 Reply Last reply
                0
                • Please_Help_me_DP Please_Help_me_D

                  @mrjj Why do I return zero?
                  Are the comments below are true?

                  int MyModel::rowCount(const QModelIndex &parent) const
                  {
                      if (!parent.isValid()) { // if this is  a root (there is no parent)
                          return _items.size(); // this returns 1 and the application goes here
                      }
                      const MyItem* parentItem = static_cast<const MyItem*>(parent.internalPointer()); // 
                  
                      return parentItem->children.size(); //returns zero but the application never reach there
                  }
                  
                  Please_Help_me_DP Offline
                  Please_Help_me_DP Offline
                  Please_Help_me_D
                  wrote on last edited by
                  #7

                  @Please_Help_me_D Oh no, I'm wrong...

                  mrjjM 1 Reply Last reply
                  0
                  • Please_Help_me_DP Please_Help_me_D

                    @Please_Help_me_D Oh no, I'm wrong...

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @Please_Help_me_D
                    well
                    you return parentItem->children.size()
                    and that is zero as it has no children :)

                    So not sure if this is a tree or list or table model ?

                    Please_Help_me_DP 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @Please_Help_me_D
                      well
                      you return parentItem->children.size()
                      and that is zero as it has no children :)

                      So not sure if this is a tree or list or table model ?

                      Please_Help_me_DP Offline
                      Please_Help_me_DP Offline
                      Please_Help_me_D
                      wrote on last edited by
                      #9

                      @mrjj it should be tree model

                      mrjjM 1 Reply Last reply
                      0
                      • Please_Help_me_DP Please_Help_me_D

                        @mrjj it should be tree model

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @Please_Help_me_D
                        Then you should add more than the first item :)
                        So the root item does have children.

                        Please_Help_me_DP 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @Please_Help_me_D
                          Then you should add more than the first item :)
                          So the root item does have children.

                          Please_Help_me_DP Offline
                          Please_Help_me_DP Offline
                          Please_Help_me_D
                          wrote on last edited by
                          #11

                          @mrjj Well I add few children to item:

                          void MyModel::fetchRootDirectory()
                          {
                              MyItem* item = new MyItem("first_item");
                              item->children.push_back(new MyItem("first_child"));
                              item->children.push_back(new MyItem("second_child"));
                              _items.push_back(item);
                          }
                          

                          but it still doesn't work :)

                          mrjjM 1 Reply Last reply
                          0
                          • Please_Help_me_DP Please_Help_me_D

                            @mrjj Well I add few children to item:

                            void MyModel::fetchRootDirectory()
                            {
                                MyItem* item = new MyItem("first_item");
                                item->children.push_back(new MyItem("first_child"));
                                item->children.push_back(new MyItem("second_child"));
                                _items.push_back(item);
                            }
                            

                            but it still doesn't work :)

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @Please_Help_me_D
                            so check with debugger what you return as rowCount ?

                            Please_Help_me_DP 2 Replies Last reply
                            0
                            • mrjjM mrjj

                              @Please_Help_me_D
                              so check with debugger what you return as rowCount ?

                              Please_Help_me_DP Offline
                              Please_Help_me_DP Offline
                              Please_Help_me_D
                              wrote on last edited by Please_Help_me_D
                              #13

                              @mrjj I wrote:

                              int MyModel::rowCount(const QModelIndex &parent) const
                              {
                                  if (!parent.isValid()) {
                                      qDebug() << "Item size: " << _items.size();
                                      return _items.size();
                                  }
                                  const MyItem* parentItem = static_cast<const MyItem*>(parent.internalPointer());
                                  qDebug() << "Children size: "  << parentItem->children.size();
                                  return parentItem->children.size();
                              }
                              

                              The output is:

                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Children size:  2
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              Children size:  2
                              Item size:  1
                              Item size:  1
                              Item size:  1
                              

                              Why the program goes to rowCount so many times? I already suspected that when I set the breakpoint and tried to contue application run to next breakpoint. Is it normal?

                              JonBJ 1 Reply Last reply
                              0
                              • Please_Help_me_DP Please_Help_me_D

                                @mrjj I wrote:

                                int MyModel::rowCount(const QModelIndex &parent) const
                                {
                                    if (!parent.isValid()) {
                                        qDebug() << "Item size: " << _items.size();
                                        return _items.size();
                                    }
                                    const MyItem* parentItem = static_cast<const MyItem*>(parent.internalPointer());
                                    qDebug() << "Children size: "  << parentItem->children.size();
                                    return parentItem->children.size();
                                }
                                

                                The output is:

                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Children size:  2
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                Children size:  2
                                Item size:  1
                                Item size:  1
                                Item size:  1
                                

                                Why the program goes to rowCount so many times? I already suspected that when I set the breakpoint and tried to contue application run to next breakpoint. Is it normal?

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #14

                                @Please_Help_me_D said in Need help to create simple model for QTreeModelView:

                                Why it gives me such many outputs?

                                Because your rowCount() gets called by Qt (e.g. the QTreeView) more times than you think it does :)

                                Please_Help_me_DP 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @Please_Help_me_D said in Need help to create simple model for QTreeModelView:

                                  Why it gives me such many outputs?

                                  Because your rowCount() gets called by Qt (e.g. the QTreeView) more times than you think it does :)

                                  Please_Help_me_DP Offline
                                  Please_Help_me_DP Offline
                                  Please_Help_me_D
                                  wrote on last edited by Please_Help_me_D
                                  #15

                                  @JonB and is it normal? or my code looks so scarry that Qt don't know what to do whith it :)

                                  JonBJ 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @Please_Help_me_D
                                    so check with debugger what you return as rowCount ?

                                    Please_Help_me_DP Offline
                                    Please_Help_me_DP Offline
                                    Please_Help_me_D
                                    wrote on last edited by
                                    #16

                                    @mrjj maybe I should manually insert rows somehow?

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

                                      Hi
                                      Im not sure what is wrong as could also be the index but you can take a look at
                                      https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
                                      which seems very similar to your code.

                                      Please_Help_me_DP 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        Hi
                                        Im not sure what is wrong as could also be the index but you can take a look at
                                        https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html
                                        which seems very similar to your code.

                                        Please_Help_me_DP Offline
                                        Please_Help_me_DP Offline
                                        Please_Help_me_D
                                        wrote on last edited by
                                        #18

                                        @mrjj ok
                                        How do you think, is it possible that the problem is how I declare variables (pointers/references and const)?. Sometimes they differ from examples but I think it should be fine

                                        mrjjM 1 Reply Last reply
                                        0
                                        • Please_Help_me_DP Please_Help_me_D

                                          @mrjj ok
                                          How do you think, is it possible that the problem is how I declare variables (pointers/references and const)?. Sometimes they differ from examples but I think it should be fine

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by mrjj
                                          #19

                                          @Please_Help_me_D
                                          well i think its fine since app compiles etc.
                                          Its more that if you use internalPointer as in the example so it does point to what you expect.

                                          Please_Help_me_DP 1 Reply Last reply
                                          0
                                          • Please_Help_me_DP Please_Help_me_D

                                            @JonB and is it normal? or my code looks so scarry that Qt don't know what to do whith it :)

                                            JonBJ Offline
                                            JonBJ Offline
                                            JonB
                                            wrote on last edited by
                                            #20

                                            @Please_Help_me_D said in Need help to create simple model for QTreeModelView:

                                            @JonB and is it normal? or my code looks so scarry that Qt don't know what to do whith it :)

                                            No, it's normal that infrastructure will call your rowCount() a lot :)

                                            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