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. QLists with signed int instead of unsigned int?

QLists with signed int instead of unsigned int?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 716 Views 2 Watching
  • 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.
  • A Offline
    A Offline
    Alex-Krieg
    wrote on last edited by
    #1

    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

    Pl45m4P 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • A Alex-Krieg

        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

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

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


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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.

          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