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. QList index

QList index

Scheduled Pinned Locked Moved Solved General and Desktop
qlistindex
16 Posts 7 Posters 6.8k 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.
  • W Offline
    W Offline
    Walux
    wrote on 23 Jun 2016, 19:29 last edited by
    #1

    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 23 Jun 2016, 19:54
    0
    • W Walux
      23 Jun 2016, 19:29

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

      And thank you ;)

      ? Offline
      ? Offline
      A Former User
      wrote on 23 Jun 2016, 19:54 last edited by
      #2

      @Walux Why would you want to do this?

      W 1 Reply Last reply 24 Jun 2016, 00:43
      1
      • J Offline
        J Offline
        Joel Bodenmann
        wrote on 23 Jun 2016, 20:09 last edited by Joel Bodenmann
        #3

        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 24 Jun 2016, 00:38
        2
        • J Joel Bodenmann
          23 Jun 2016, 20:09

          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?

          W Offline
          W Offline
          Walux
          wrote on 24 Jun 2016, 00:38 last edited by
          #4

          @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
          0
          • ? A Former User
            23 Jun 2016, 19:54

            @Walux Why would you want to do this?

            W Offline
            W Offline
            Walux
            wrote on 24 Jun 2016, 00:43 last edited by
            #5

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

            K 1 Reply Last reply 24 Jun 2016, 10:28
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 24 Jun 2016, 07:27 last edited by
              #6

              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
              3
              • W Walux
                24 Jun 2016, 00:43

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

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 24 Jun 2016, 10:28 last edited by
                #7

                @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

                J 1 Reply Last reply 24 Jun 2016, 11:00
                1
                • K kshegunov
                  24 Jun 2016, 10:28

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

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 24 Jun 2016, 11:00 last edited by
                  #8

                  @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

                  K 1 Reply Last reply 24 Jun 2016, 11:11
                  1
                  • J jsulm
                    24 Jun 2016, 11:00

                    @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

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 24 Jun 2016, 11:11 last edited by
                    #9

                    @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

                    J 1 Reply Last reply 24 Jun 2016, 11:13
                    0
                    • K kshegunov
                      24 Jun 2016, 11:11

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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 24 Jun 2016, 11:13 last edited by
                      #10

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

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

                      M J 2 Replies Last reply 24 Jun 2016, 12:52
                      0
                      • J jsulm
                        24 Jun 2016, 11:13

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

                        J Offline
                        J Offline
                        Joel Bodenmann
                        wrote on 24 Jun 2016, 13:31 last edited by Joel Bodenmann
                        #11

                        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
                        1
                        • W Offline
                          W Offline
                          Walux
                          wrote on 24 Jun 2016, 13:48 last edited by Walux
                          #12

                          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
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 24 Jun 2016, 13:50 last edited by
                            #13

                            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 24 Jun 2016, 13:53
                            1
                            • S SGaist
                              24 Jun 2016, 13:50

                              Again: why must they start with 1 ?

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

                              W Offline
                              W Offline
                              Walux
                              wrote on 24 Jun 2016, 13:53 last edited by
                              #14

                              @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
                              0
                              • Chris KawaC Offline
                                Chris KawaC Offline
                                Chris Kawa
                                Lifetime Qt Champion
                                wrote on 24 Jun 2016, 19:00 last edited by
                                #15

                                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 25 Jun 2016, 00:19
                                3
                                • Chris KawaC Chris Kawa
                                  24 Jun 2016, 19:00

                                  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 Offline
                                  W Offline
                                  Walux
                                  wrote on 25 Jun 2016, 00:19 last edited by
                                  #16

                                  @Chris-Kawa

                                  Got it ;)

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

                                  1 Reply Last reply
                                  0

                                  5/16

                                  24 Jun 2016, 00:43

                                  topic:navigator.unread, 11
                                  • Login

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