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. Increment by one
Qt 6.11 is out! See what's new in the release blog

Increment by one

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 3.5k Views 1 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.
  • M Offline
    M Offline
    musti
    wrote on last edited by
    #1

    hello i made a calculator white table that archive all the calculation the code bellow is for the table and it work only for row = 0 namely the first row i tried to increment y with (y++) but it does not work :( .

    @
    int y=0;

    if (operationBool){

       if (row == y && col == 0  ) 
                                { return int  (fNum);  }  //  fNum = number 
    
       if(addBool== 1 )
                             {if( row == y && col==1) return  QString("+");}
    
       else if (divideBool)
                             {if( row == y && col==1) return  QString("/");}
    
       else if (subtractbool)
                              {if  ( row == y && col==1) return  QString("-");}
    
       else if (multiplyBool)
                               {if  ( row == y && col==1) return  QString("x");}
    
        if (row == y && col == 2  ) { return int(SNum);}
    
           if(row == y && col ==4)return int (tot);
    

    y++;
    }
    @
    i have to say that i'm new in programming so please help me thank you :)

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jyang772
      wrote on last edited by
      #2

      Where are you incrementing y?

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        And please, do something about indenting!! This is terrible to read!!
        Where is your y++??

        Greetz, Jeroen

        1 Reply Last reply
        0
        • M Offline
          M Offline
          musti
          wrote on last edited by
          #4

          i edited the code sorry for the problem .

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            Hi,
            What should the program do??
            In what kind of function do you place your code snippet?
            For indenting I would use this:
            @
            int y=0;

            if (operationBool){

               if ( (row == y) && 
                    (col == 0)  )
               { 
                    return int  (fNum);   //  fNum = number
               }
            

            // etc etc......
            // Only 1 comparison per line, use () around every single one.
            y++; // You are incrementing the y, but You only do the if ones??
            }
            @
            Why do you increment y? There is only a single if (operationbool), so y will be destroyed when you exit your function. The if statement is only executed ones.

            Greetz, Jeroen

            1 Reply Last reply
            0
            • M Offline
              M Offline
              musti
              wrote on last edited by
              #6

              hi
              i want to increment " y " to pass to the next row the first row is row y ,where y =0,
              i want to increment y to become y=1 ; and keep it like that until the next operation . so when (operationBool is true again ) y is equal to 1 not zero , so you were right when you sad that y will be destroyed after i exit the function ,
              what should i do to keep y = 1 after i exit the function ?
              thank you

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                Hi,
                You should keep y in memory ;-)
                If you want to do it dirty, you make y a global scope variable, if you want it a nice, you make it a member variable on your class.
                IMHO you should first read a good programming book, understand variable scope etc and building classes and so on.

                Greetz, Jeroen

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  musti
                  wrote on last edited by
                  #8

                  hello :)
                  i defined y as a : static int y ;// in header file
                  then in cpp file i defined int MyModel :: y = 0 ;
                  and the code became :

                  @QVariant MyModel::data(const QModelIndex &index, int role)const
                  { int row = index.row();
                  int col = index.column();

                  qDebug() << QString("row %1, col%2, role %3")
                  .arg(row).arg(col).arg(role);

                  switch(role){
                  case Qt::DisplayRole:

                  if(row && col==3) return QString(" = "); // will type "=" in all the row and the column 3 ;

                  if (operationBool){ // when equal button is released operationBool == true

                  if (row==y && col == 0 )
                  return int(fNum); // on the first row and first column write the value of the variable fNum

                  if(addBool ) // add buttomn is released write + in the first row and first col
                  if( row==y && col==1) return QString("+");

                  else if (divideBool)
                  if( row==y && col==1) return QString("/");

                  else if (subtractbool)
                  if( row==y && col==1) return QString("-");

                  else if (multiplyBool)
                  if( row==y&& col==1) return QString("x");

                  if (row==y && col == 2 )
                  return int(SNum);// write the second value of my operation

                  if(row==y && col ==4)return int (tot);} // write the total

                              break;
                  

                  }
                  return QVariant();
                  }
                  @
                  now were i should add my y++ ?
                  what i want to do is going to the next row every time i release the equal button .
                  i tried to make y as global int but it didn't work :(
                  i'd like to read a good book about c++ but i need to represent this project as a final exam the next week .
                  thank you :) :)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    musti
                    wrote on last edited by
                    #9

                    it work finally and this is the code but i still have a problem that every time i raise the row the previous row disappear
                    @
                    QVariant MyModel::data(const QModelIndex &index, int role)const
                    { int row = index.row();
                    int col = index.column();
                    for (int i = 0 ; i<9 ; i++ ){
                    if(row==i && col==3) return QString(" = ");
                    }

                    if (equalesbool){

                    if (row==y && col == 0 ) return int(fNum);

                    if(divideBool){
                    if( row==y && col==1) return QString ("div");
                    divideBool=false ;}

                    else if (addBool){

                    if( row==y && col==1) return QString("add");
                    addBool=false ;

                    }

                    else if(subtractbool==1){

                    if( row==y && col==1) return QString("sub");
                    subtractbool=false;
                    }

                    else if (multiplyBool) {

                    if( row==y&& col==1) return QString("mult");

                    multiplyBool=false;}

                    if (row==y && col == 2 ) return int(SNum);

                    if(row==y && col ==4)return int (tot);

                    if (opBool){
                    y ++;
                    opBool = false;}

                    }

                    return QVariant();

                        };
                    

                    @

                    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