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.

qTreeView with custom model not working.

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 958 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

    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.

    eyllanescE Offline
    eyllanescE Offline
    eyllanesc
    wrote on last edited by eyllanesc
    #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 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
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on 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 last edited by superdu
          #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.

          eyllanescE 1 Reply Last reply
          0
          • S superdu

            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.

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on 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 last edited by superdu
              #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
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on 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 last edited by superdu
                  #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
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 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 last edited by
                      #15

                      can i store classes as QVariant?

                      jsulmJ VRoninV 2 Replies Last reply
                      0
                      • S superdu

                        can i store classes as QVariant?

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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

                          can i store classes as QVariant?

                          VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on 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 last edited by
                            #18

                            thank you i will try that.

                            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