Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QList index

    General and Desktop
    qlist index
    7
    16
    5152
    Loading More Posts
    • 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.
    • W
      Walux last edited by

      The answer must be obvious , but how do i make a QList's index start with 1 instead of 0 .

      And thank you ;)

      Taking things from beginning to end : That's my entertainment !

      ? 1 Reply Last reply Reply Quote 0
      • ?
        A Former User @Walux last edited by

        @Walux Why would you want to do this?

        W 1 Reply Last reply Reply Quote 1
        • Joel Bodenmann
          Joel Bodenmann last edited by Joel Bodenmann

          The fact that container indexes start at zero is a language feature (I might be wrong?). Anyway, fact is that you can't just change it.
          What you can do is subclassing the container class and overloading the operator[] to add an offset. Note: That is a possible solution and not a recommendation.

          But as @Wieland already asked... Why would you want to do this?

          Industrial process automation software: https://simulton.com
          Embedded Graphics & GUI library: https://ugfx.io

          W 1 Reply Last reply Reply Quote 2
          • W
            Walux @Joel Bodenmann last edited by

            @Joel-Bodenmann

            Thanks for your support , it's probably not the smoothest idea , but it's good enough .

            Taking things from beginning to end : That's my entertainment !

            1 Reply Last reply Reply Quote 0
            • W
              Walux @Guest last edited by

              @Wieland

              Well , all i can say is that in my program i used a lot of QLists that are better off starting with 1 as an index , it would really make the work - and especially the code - more comfortable and readable .

              Taking things from beginning to end : That's my entertainment !

              kshegunov 1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Hi,

                That's a pretty wrong idea. Your code is going to be understandable only by you and hard to debug for other people.

                All list/vector like containers are indexed at 0. You seem to try to work-around something else. What is it that makes your list related code "better off starting at 1" ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 3
                • kshegunov
                  kshegunov Moderators @Walux last edited by

                  @Walux

                  are better off starting with 1 as an index

                  This is what fortran does (and a few other obscure and useless languages) but it's arbitrary and completely artificial. An index in an array is the offset from the beginning of that array, so the first element has an offset of 0. There's no real, practical or good reason to think that arrays or lists should start from 1 just because people are used to counting that way; as I said such reasoning is arbitrary and is introduced completely artificially.

                  Read and abide by the Qt Code of Conduct

                  jsulm 1 Reply Last reply Reply Quote 1
                  • jsulm
                    jsulm Lifetime Qt Champion @kshegunov last edited by

                    @kshegunov Hey, I learned programming with Turbo Pascal (it's not obscure or useless for me :-)) where you can define whether first index is 0 or 1

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

                    kshegunov 1 Reply Last reply Reply Quote 1
                    • kshegunov
                      kshegunov Moderators @jsulm last edited by

                      @jsulm
                      Actually my beef is with fortran, but you're right of course ... ;)
                      Still, C uses the zero-based (offset based) indexing and changing that would go against the language itself (not only against Qt) ... so there's no good reason to do (or even want) it.

                      Read and abide by the Qt Code of Conduct

                      jsulm 1 Reply Last reply Reply Quote 0
                      • jsulm
                        jsulm Lifetime Qt Champion @kshegunov last edited by

                        @kshegunov I agree with you: there is no need to redefine this behaviour.

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

                        mrjj Joel Bodenmann 2 Replies Last reply Reply Quote 0
                        • Joel Bodenmann
                          Joel Bodenmann @jsulm last edited by Joel Bodenmann

                          The "rest" of this topic got split into a separate topic.
                          Enjoy.

                          Industrial process automation software: https://simulton.com
                          Embedded Graphics & GUI library: https://ugfx.io

                          1 Reply Last reply Reply Quote 1
                          • W
                            Walux last edited by Walux

                            Thanks you all for your contribution :)

                            I think i'm now convinced that the QList's index must start with 0 , even tho that was not the goal of this topic :b

                            To help make the image clear , i used plenty of variables that MUST start with 1 , and these variables are connected to a lot of arrays , what i do now is create NULL variables and store them in the beginning of the arrays i wish to use its items from "1" .

                            For example :

                            int index0 = 0
                            QList<int> myArray;
                            myArray << index0 << myInteger1 << ...
                            
                            

                            But , is it safe all the time ?

                            Taking things from beginning to end : That's my entertainment !

                            1 Reply Last reply Reply Quote 0
                            • SGaist
                              SGaist Lifetime Qt Champion last edited by

                              Again: why must they start with 1 ?

                              A side effect of your current implementation is that you are wasting memory.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              W 1 Reply Last reply Reply Quote 1
                              • W
                                Walux @SGaist last edited by

                                @SGaist

                                Hmmm , i guess that i should start accepting the way the arrays are built instead of making stubborn statements , thank you all for your advices .

                                I'll mark the topic as solved.

                                Taking things from beginning to end : That's my entertainment !

                                1 Reply Last reply Reply Quote 0
                                • Chris Kawa
                                  Chris Kawa Moderators last edited by

                                  To add to all the excellent points - stuffing an artificial null element just to try to index from 1 will break as soon as you do e.g. myArray.clear() or try to iterate with myArray.begin(), use a range based for or tons of other code types. c++ is 0 based language. Don't swim against the tide.

                                  Btw. calling a QList myArray is like calling a sausage chain a nunchaku ;) Although similar at first glance they are completely different things. You'll mislead readers of your code. Don't do that.

                                  W 1 Reply Last reply Reply Quote 3
                                  • W
                                    Walux @Chris Kawa last edited by

                                    @Chris-Kawa

                                    Got it ;)

                                    Taking things from beginning to end : That's my entertainment !

                                    1 Reply Last reply Reply Quote 0
                                    • First post
                                      Last post