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
QtWS: Super Early Bird Tickets Available!

QList index

Scheduled Pinned Locked Moved Solved General and Desktop
qlistindex
16 Posts 7 Posters 6.3k 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 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
    0
  • ? Offline
    ? Offline
    A Former User
    replied to Walux on last edited by
    #2

    @Walux Why would you want to do this?

    W 1 Reply Last reply
    1
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on 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
    2
  • W Offline
    W Offline
    Walux
    replied to Joel Bodenmann on 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
  • W Offline
    W Offline
    Walux
    replied to A Former User on 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 !

    kshegunovK 1 Reply Last reply
    0
  • SGaistS Offline
    SGaistS Offline
    SGaist Lifetime Qt Champion
    wrote on 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
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov Moderators
    replied to Walux on 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

    jsulmJ 1 Reply Last reply
    1
  • jsulmJ Online
    jsulmJ Online
    jsulm Lifetime Qt Champion
    replied to kshegunov on 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

    kshegunovK 1 Reply Last reply
    1
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov Moderators
    replied to jsulm on 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

    jsulmJ 1 Reply Last reply
    0
  • jsulmJ Online
    jsulmJ Online
    jsulm Lifetime Qt Champion
    replied to kshegunov on 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

    mrjjM Joel BodenmannJ 2 Replies Last reply
    0
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    replied to jsulm on 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 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
  • SGaistS Offline
    SGaistS Offline
    SGaist Lifetime Qt Champion
    wrote on 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
    1
  • W Offline
    W Offline
    Walux
    replied to SGaist on 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 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
    3
  • W Offline
    W Offline
    Walux
    replied to Chris Kawa on last edited by
    #16

    @Chris-Kawa

    Got it ;)

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

    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