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. qTreeView with custom model not working.
Forum Updated to NodeBB v4.3 + New Features

qTreeView with custom model not working.

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 970 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.
  • S superdu
    4 Aug 2021, 00:50

    i read the documentation and i understand the meaning of beginInsertRows() and endInsertRows(). but i dont think its the problem. indeed if you check TreeModel.cpp the headerData method return just a test element but the treeview dont even show header informations.

    E Offline
    E Offline
    eyllanesc
    wrote on 4 Aug 2021, 01:54 last edited by eyllanesc 8 Apr 2021, 01:56
    #6

    @superdu Your code has several problems like the one I already pointed out to you. The other problem that you indicate is that the columnCount (which is the same number of headers) depends on the rootItem, rootItem->columnCount() is 0 so the number of columns(and headers) is 0. Make the columnCount() of rootItem at least 1 and you will see that the header will appear

    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

    1 Reply Last reply
    1
    • S Offline
      S Offline
      superdu
      wrote on 4 Aug 2021, 11:17 last edited by
      #7

      ok so i tried what you said to just return 1 at the columnCount but nothing happen. For your first comment, its already implemented in TreeModel::insertRows so i could use this method to add rows than directly add child to the root but i dont know how to use QModelIndex. What should i store in?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 4 Aug 2021, 13:29 last edited by
        #8

        In both rowCount() and columnCount() you have:

        if (!parent.isValid())
                return 0;
        

        The top level item has an invalid parent so your model will always have 0 rows and 0 columns.

        P.S.
        When implementing a new model, always run it through the model test it's an invaluable resource for spotting mistakes

        "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
        4
        • S Offline
          S Offline
          superdu
          wrote on 4 Aug 2021, 15:31 last edited by superdu 8 Apr 2021, 15:34
          #9

          Thank you for your help and your tips. i deleted this if statements from this methodes but that still not working. I already tried to use QAbstractItemModelTester but that doesn't return any problems. maybe im not using it in the good way. I also tried with the three FailureReportingMode but notihng. its like if the model is not connected to the view at all but i dont understand why. i put some qdebug in the model's methods but they are no any outputs of them from the header method.

          E 1 Reply Last reply 4 Aug 2021, 18:47
          0
          • S superdu
            4 Aug 2021, 15:31

            Thank you for your help and your tips. i deleted this if statements from this methodes but that still not working. I already tried to use QAbstractItemModelTester but that doesn't return any problems. maybe im not using it in the good way. I also tried with the three FailureReportingMode but notihng. its like if the model is not connected to the view at all but i dont understand why. i put some qdebug in the model's methods but they are no any outputs of them from the header method.

            E Offline
            E Offline
            eyllanesc
            wrote on 4 Aug 2021, 18:47 last edited by
            #10

            @superdu Recommendation: Use the base example and modify it with the new features, do not create everything from scratch if it is the first time you try.

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            1 Reply Last reply
            3
            • S Offline
              S Offline
              superdu
              wrote on 4 Aug 2021, 19:20 last edited by superdu 8 Apr 2021, 19:21
              #11

              thank you for your advice but it's more or less what i did already ^^. the problem is the fact that for the official example they read text file which is different from what i'm looking for. But if you check the example you will see that my classes have same names and definitions of methodes are praticly the same than the official example. maybe you have other examples which can help me?

              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 4 Aug 2021, 23:37 last edited by
                #12

                If you want another example you can have a look at this implementation but I fear it’s a bit complicated for your stage.
                Can I ask why you decided to go down the hell of making a model instead of using a ready-made one (like QStandardItemModel)?

                "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
                • S Offline
                  S Offline
                  superdu
                  wrote on 4 Aug 2021, 23:47 last edited by superdu 8 Apr 2021, 23:56
                  #13

                  yep, i tried standardmodel and that works but my items can store more things than strings so i did a class which inherits standard item class. after that i dont remember well why but i didn't arrived to do what i want with standard model because of type error. I foud a potential solution on internet but it was not elegant. So i thought about developping my own model for learn and for have the possibility to complexify the widget in the future . thank you for your example by the way

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 5 Aug 2021, 00:19 last edited by
                    #14

                    QStandardItem can handle all data types, take a look at the setData method.

                    "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
                    1
                    • S Offline
                      S Offline
                      superdu
                      wrote on 5 Aug 2021, 00:21 last edited by
                      #15

                      can i store classes as QVariant?

                      J V 2 Replies Last reply 5 Aug 2021, 04:34
                      0
                      • S superdu
                        5 Aug 2021, 00:21

                        can i store classes as QVariant?

                        J Online
                        J Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on 5 Aug 2021, 04:34 last edited by
                        #16

                        @superdu setData suggested by @VRonin takes a QVariant as parameter...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • S superdu
                          5 Aug 2021, 00:21

                          can i store classes as QVariant?

                          V Offline
                          V Offline
                          VRonin
                          wrote on 5 Aug 2021, 08:43 last edited by
                          #17

                          @superdu Yes, see https://doc.qt.io/qt-5/custom-types.html

                          "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
                          2
                          • S Offline
                            S Offline
                            superdu
                            wrote on 5 Aug 2021, 12:30 last edited by
                            #18

                            thank you i will try that.

                            1 Reply Last reply
                            0

                            15/18

                            5 Aug 2021, 00:21

                            • Login

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