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. Struggling with a for loop
Forum Updated to NodeBB v4.3 + New Features

Struggling with a for loop

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 281 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    Hi,
    Sorry for this silly question but i spent more than 2 hours without success

    My problem is :
    i have a QList of items, the number of items is a multiple of a variable nbrOperationToRepeat
    items have a method setPosX(int)

    so lets say nbrOperationToRepeat = 3;
    and i have 9 items

    i want to iterate over the list and call setX() every 3 (nbrOperationToRepeat) items,

    so the x of my items should be :

    item.at(0) > 0
    item.at(1) > 0
    item.at(2) > 0

    item.at(3) > 1
    item.at(4) > 1
    item.at(5) > 1

    item.at(6) > 2
    item.at(7) > 2
    item.at(8) > 2

    i feel like this sould be an easy task, but i'm not able to do it right now, can someone please help with this ?

    for (int i = 0; i < finalOperationList.count(); i+=nbrOperationToRepeat ){ // finalOperationList.count() returns the number of items in the list
    
                    for(int j = 0; j < nbrOperationToRepeat; j++){
                          qDebug()<< "edit at: " << i+j;
                           finalOperationList.at(i+j)->setPosX(/*?*/); // cant figure out what to put here.. 
                    }
                }
    

    thank you

    jsulmJ J.HilkJ KroMignonK 3 Replies Last reply
    0
    • ODБOïO ODБOï

      Hi,
      Sorry for this silly question but i spent more than 2 hours without success

      My problem is :
      i have a QList of items, the number of items is a multiple of a variable nbrOperationToRepeat
      items have a method setPosX(int)

      so lets say nbrOperationToRepeat = 3;
      and i have 9 items

      i want to iterate over the list and call setX() every 3 (nbrOperationToRepeat) items,

      so the x of my items should be :

      item.at(0) > 0
      item.at(1) > 0
      item.at(2) > 0

      item.at(3) > 1
      item.at(4) > 1
      item.at(5) > 1

      item.at(6) > 2
      item.at(7) > 2
      item.at(8) > 2

      i feel like this sould be an easy task, but i'm not able to do it right now, can someone please help with this ?

      for (int i = 0; i < finalOperationList.count(); i+=nbrOperationToRepeat ){ // finalOperationList.count() returns the number of items in the list
      
                      for(int j = 0; j < nbrOperationToRepeat; j++){
                            qDebug()<< "edit at: " << i+j;
                             finalOperationList.at(i+j)->setPosX(/*?*/); // cant figure out what to put here.. 
                      }
                  }
      

      thank you

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @LeLev

      for (int i = 0; i < finalOperationList.count() / nbrOperationToRepeat; i++){
           for(int j = i * nbrOperationToRepeat; (j < i + nbrOperationToRepeat) && (j < finalOperationList.count()); j++){
               qDebug()<< "edit at: " << j;
               finalOperationList.at(j)->setPosX(i);
           }
       }
      

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

      1 Reply Last reply
      2
      • ODБOïO ODБOï

        Hi,
        Sorry for this silly question but i spent more than 2 hours without success

        My problem is :
        i have a QList of items, the number of items is a multiple of a variable nbrOperationToRepeat
        items have a method setPosX(int)

        so lets say nbrOperationToRepeat = 3;
        and i have 9 items

        i want to iterate over the list and call setX() every 3 (nbrOperationToRepeat) items,

        so the x of my items should be :

        item.at(0) > 0
        item.at(1) > 0
        item.at(2) > 0

        item.at(3) > 1
        item.at(4) > 1
        item.at(5) > 1

        item.at(6) > 2
        item.at(7) > 2
        item.at(8) > 2

        i feel like this sould be an easy task, but i'm not able to do it right now, can someone please help with this ?

        for (int i = 0; i < finalOperationList.count(); i+=nbrOperationToRepeat ){ // finalOperationList.count() returns the number of items in the list
        
                        for(int j = 0; j < nbrOperationToRepeat; j++){
                              qDebug()<< "edit at: " << i+j;
                               finalOperationList.at(i+j)->setPosX(/*?*/); // cant figure out what to put here.. 
                        }
                    }
        

        thank you

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

        @LeLev said in Struggling with a for loop:

        for (int i = 0; i < finalOperationList.count(); i+=nbrOperationToRepeat ){ // finalOperationList.count() returns the number of items in the list
        
                        for(int j = 0; j < nbrOperationToRepeat; j++){
                              qDebug()<< "edit at: " << i+j;
                               finalOperationList.at(i+j)->setPosX(i / nbrOperationToRepeat); //if nbrOperationToRepeat is a int and not a double or float
                        }
                    }
        

        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
        1
        • ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          @jsulm , @J-Hilk ,

          Thank you very much for your inputs much appreciated

          1 Reply Last reply
          1
          • ODБOïO ODБOï

            Hi,
            Sorry for this silly question but i spent more than 2 hours without success

            My problem is :
            i have a QList of items, the number of items is a multiple of a variable nbrOperationToRepeat
            items have a method setPosX(int)

            so lets say nbrOperationToRepeat = 3;
            and i have 9 items

            i want to iterate over the list and call setX() every 3 (nbrOperationToRepeat) items,

            so the x of my items should be :

            item.at(0) > 0
            item.at(1) > 0
            item.at(2) > 0

            item.at(3) > 1
            item.at(4) > 1
            item.at(5) > 1

            item.at(6) > 2
            item.at(7) > 2
            item.at(8) > 2

            i feel like this sould be an easy task, but i'm not able to do it right now, can someone please help with this ?

            for (int i = 0; i < finalOperationList.count(); i+=nbrOperationToRepeat ){ // finalOperationList.count() returns the number of items in the list
            
                            for(int j = 0; j < nbrOperationToRepeat; j++){
                                  qDebug()<< "edit at: " << i+j;
                                   finalOperationList.at(i+j)->setPosX(/*?*/); // cant figure out what to put here.. 
                            }
                        }
            

            thank you

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

            @LeLev said in Struggling with a for loop:

            I would work with modulus operator:

            for (int i = 0; i < finalOperationList.count();  ++i){
               int operationTodo = i % nbrOperationToRepeat;
               qDebug()<< "edit at: " << i << "and set:" << operationTodo ;
               finalOperationList.at(i)->setPosX(operationTodo);
            }
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            2

            • Login

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