Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Use int type or qint32 as a loop iterator
QtWS25 Last Chance

Use int type or qint32 as a loop iterator

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 4 Posters 823 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.
  • Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by
    #1

    Hi,

    More philosophical question.
    As far as I know Qt standard types like quint16, qint32, qin64 etc guarantee that it take 2, 4 and 8 bytes respectively to store every number on any type of machine.
    But there is standart loop like:

    for (int i; i < 100; i++){
        //body of loop
    }
    

    that is usually written with iterator type int (in every code). Usually it is not a problem if on a developer computer type int is a 4-byte integer and on another it may take 8 bytes (are there such situations?). But from the side of politeness to other developpers is it ok to write loops with Qt stadart types ?? like:

    for (qint32 i; i < 100; i++){
        //body of loop
    }
    

    I'm sorry if I don't fully understand the difference between int and qint32

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

      @Please_Help_me_D said in Use int type or qint32 as a loop iterator:

      I'm sorry if I don't fully understand the difference between int and qint32

      There isn't a difference.

      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
      3
      • Please_Help_me_DP Please_Help_me_D

        Hi,

        More philosophical question.
        As far as I know Qt standard types like quint16, qint32, qin64 etc guarantee that it take 2, 4 and 8 bytes respectively to store every number on any type of machine.
        But there is standart loop like:

        for (int i; i < 100; i++){
            //body of loop
        }
        

        that is usually written with iterator type int (in every code). Usually it is not a problem if on a developer computer type int is a 4-byte integer and on another it may take 8 bytes (are there such situations?). But from the side of politeness to other developpers is it ok to write loops with Qt stadart types ?? like:

        for (qint32 i; i < 100; i++){
            //body of loop
        }
        

        I'm sorry if I don't fully understand the difference between int and qint32

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Please_Help_me_D like @Christian-Ehrlicher said, there is no real difference,
        but most access functions, like for example QVector::at(int), expect an int.

        So you will get a warning, when using the variable inside the loop, in such a fashion, and you'll have to cast it, to silence the warning.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • Please_Help_me_DP Offline
          Please_Help_me_DP Offline
          Please_Help_me_D
          wrote on last edited by
          #4

          @Christian-Ehrlicher and @J-Hilk thank you!

          1 Reply Last reply
          1
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            If coding for the platform then it's ok to use the Qt types. If the function is more generic then use the equivalent standard types. As for myself: I generally use standard types even if Qt aliases them.

            1 Reply Last reply
            1

            • Login

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