Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLists with signed int instead of unsigned int?



  • Why does QT use signed int for indexing an QList or other form of listed object?

    QList<double> list;
    int index = 0;
    
    list[index]  = 1.05;
    

    Arrays always start at Index 0, therefore I can't see any reason, why signed int is used.
    The same applies for the return value of member functions like:

    list.size();
    

    The list can't be smaller than 0.

    Is there any logical reason of taking signed values?
    I have to convert them everywhere, because I use always unsigned values for things which can't handle negative numbers.

    Thanks for your answer
    Alex


  • Qt Champions 2019

    Is there any logical reason of taking signed values?

    Yes, e.g. to be able to return -1 on error.
    Why do you think you need it? Are you sure you have (more than) 4GB of contiguous memory?
    You can take a look at the devel mailing list archives - this question is discussed there now and then.



  • @Alex-Krieg

    Another example:
    IDs of QButtonGroup start at -2 and decrease for every new button added to this group (default behavior).
    -1 is to auto-assign an ID. User-defined IDs should start at 0 (increasing).

    https://doc.qt.io/qt-5/qbuttongroup.html#addButton

    So there are some cases where the sign matters.
    (In addition to the -1 which is returned e.g. as error code)


  • Lifetime Qt Champion

    Hi
    Even if the other already explained it...
    Think of it as a role. The role of being an index type.
    We need sometimes to flag the index as invalid.
    And since none ever uses negative indexes, using signed can be used for that.


Log in to reply