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. How to pass multiple objects one after another in to loop..?
QtWS25 Last Chance

How to pass multiple objects one after another in to loop..?

Scheduled Pinned Locked Moved Solved General and Desktop
for loopqtablewidget
12 Posts 2 Posters 3.4k 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.
  • R Offline
    R Offline
    Rohith
    wrote on 5 Mar 2016, 09:37 last edited by
    #1

    Hi,

    I have created around 30 items using the QTableWidgetItem that cotains a chech box for each by using the below line

    
    a = new QTableWidgetItem("a");
    a->setCheckState(Qt::Unchecked);
    
    b = new QTableWidgetItem("a");
    b->setCheckState(Qt::Unchecked);
    
    and so on
    

    And i have provided a quit button and when ever the user clicks on the quit button all the chechboxes need to be unchekced weather they are checked or not checked to do so i am trying to pass all the object names one after another into a loop as shown below

    QStringList items;
    items<<"a"<<"b";
    
        for(int i=0; i<items.size(); i++)
        {
            QTableWidgetItem items.at(i) = new QTableWidgetItem;
        }
    

    and this is showing an error
    "Expected intializer before '.' token"

    Will the method i am trying to use will be possible or is there any other way todo so
    Please guide me
    Thanks in advance,
    Rohith.G

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Mar 2016, 09:53 last edited by mrjj 3 May 2016, 09:54
      #2

      Hi
      When I read the text and see the code, i get very confused.

      So im not really sure what you are trying to do.
      So I will be guessing
      you want a list of the a,b,c,d,e items so u can uncheck them.

      you can build a list this way

      int iColumns = my_table.columnCount();
      int iRows = my_table.rowCount();
      QList<QWidgetItem*> myList;
      for(int i = 0; i < iRows; ++i) {
        for(int j = 0; j < iColumn; ++j) {
          QTableWidgetItem* pWidget = my_table->item(i, j);
          myList.append(pWigdet);
        }
      }
      

      now all your items are in myList;

      You could also just call
      pWidget ->setCheckState(Qt::Unchecked);
      instead of myList.append(pWigdet); to do it directly instead of adding to list.

      I hope it answered what you ask :)

      1 Reply Last reply
      1
      • R Offline
        R Offline
        Rohith
        wrote on 5 Mar 2016, 10:15 last edited by Rohith 3 May 2016, 10:18
        #3

        Hi mrjj,

        Thanks for replying sorry for writing the question in such a enigmatic way.
        The thing i want to do is after creating multiple multiple qTableWidgetItems that can accept a selection in the form of checkboxes and when ever the user clicks on quit button all the checkboxes that are selected need to be unselected.

        a = new QTableWidgetItem("a");
        a->setCheckState(Qt::Unchecked);
        
        b = new QTableWidgetItem("b");
        b->setCheckState(Qt::Unchecked);
        
        .
        .
        .
        .
        
        z = new QTableWidgetItem("z");
        z->setCheckState(Qt::Unchecked);
        
        

        and i am unchecking them in the below way

        a->setCheckState(Qt::Unchecked);
        b->setCheckState(Qt::Unchecked);
        .
        .
        .
        z->setCheckState(Qt::Unchecked);
        
        

        To avoid huge no of lines i am trying to uncheck each item by passing the objects in to for loop as stated in above my post

        QStringList items;
        items<<"a"<<"b"<<....<<"z";
        
            for(int i=0; i<items.size(); i++)
            {
                QTableWidgetItem items.at(i) = new QTableWidgetItem;
            }
        
        

        Thanks in advance,
        Rohith.G

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 5 Mar 2016, 10:21 last edited by
          #4

          @mrjj said:
          Ok, but you have access to the items via the
          table->item(i, j);
          That is how you can get the items to uncheck them.
          What is wrong with that way?

          --
          this code makes no sense to me. Its not valid.
          Items is a stringlist so
          " QTableWidgetItem items.at(i)"
          makes no sense to me. you cannot write like that.
          Its a list of strings and have nothing to do with QTableWidgetItem.

            for(int i=0; i<items.size(); i++)
              {
                  QTableWidgetItem items.at(i) = new QTableWidgetItem;
              }
          
          
          1 Reply Last reply
          0
          • R Offline
            R Offline
            Rohith
            wrote on 5 Mar 2016, 10:27 last edited by
            #5

            Hi mrjj,

            I have tried by following your method as below

            
                int iColumns = table->columnCount();
                int iRows = table->rowCount();
            
                for(int i = 0; i < iRows; ++i) {
                  for(int j = 0; j < iColumns; ++j) {
                      qDebug("Inside the for loop");
                    QTableWidgetItem* pWidget = table->item(i, j);
                    pWidget->setCheckState(Qt::Unchecked);
            //        myList.append(pWigdet);
                  }
                }
            

            But this is compiling fine and after running when ever the quit button is clicked
            It is entering into the slot but the gui was closing by apperance of below line
            "The program has unexpectedly finished."

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 5 Mar 2016, 10:30 last edited by
              #6

              ok
              please insert check
              if (pWidget)
              pWidget->setCheckState(Qt::Unchecked);

              if it still crashes, then put break point on and see what line.

              Also, table is your table , correct ?
              You have new it and added the items to it?

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rohith
                wrote on 5 Mar 2016, 10:39 last edited by
                #7

                Hi mrjj,

                Your Code was working and sorry to say this i have written the object name and display string name differently```

                //Previous Syntax
                z = new QTableWidgetItem("z");
                z->setCheckState(Qt::Unchecked);

                //New Syntax
                z = new QTableWidgetItem("X");
                z->setCheckState(Qt::Unchecked);

                
                Can you please help me to solve as per new requirement
                M 1 Reply Last reply 5 Mar 2016, 10:45
                0
                • R Rohith
                  5 Mar 2016, 10:39

                  Hi mrjj,

                  Your Code was working and sorry to say this i have written the object name and display string name differently```

                  //Previous Syntax
                  z = new QTableWidgetItem("z");
                  z->setCheckState(Qt::Unchecked);

                  //New Syntax
                  z = new QTableWidgetItem("X");
                  z->setCheckState(Qt::Unchecked);

                  
                  Can you please help me to solve as per new requirement
                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 5 Mar 2016, 10:45 last edited by
                  #8

                  @Rohith
                  Hi
                  Good the other worked.

                  Im not sure what you ask about ?
                  you replace "z" with "X"

                  So what do you need help with?

                  R 1 Reply Last reply 5 Mar 2016, 10:56
                  0
                  • M mrjj
                    5 Mar 2016, 10:45

                    @Rohith
                    Hi
                    Good the other worked.

                    Im not sure what you ask about ?
                    you replace "z" with "X"

                    So what do you need help with?

                    R Offline
                    R Offline
                    Rohith
                    wrote on 5 Mar 2016, 10:56 last edited by
                    #9

                    @mrjj

                    The thing is that code that you have posted is working
                    if the both the object name and displaying name are same then the the operation uncheck is working it means

                    z = new QTableWidgetItem("z");
                    z->setCheckState(Qt::Unchecked);

                    in the above code z is the object name and the data present in double qoutes "z" is the data that display on GUI, your code was executing

                    QTableWidgetItem* pWidget = table->item(i, j);
                    table->item(i,j) is retreving the data that we are writing in the doublequotes which will display on GUI it was not retreving the object name of item that i have created using QTableWidgetItem *z(ObjectName) = new QTableWidgetItem("Z"(data displays on Gui));

                    if(z(pWidget)
                    z(pWidget)->setCheckState(Qt::Unchecked)

                    but know i have changed my syntax to

                    z = new QTableWidgetItem("X");
                    z->setCheckState(Qt::Unchecked);

                    Hope You understand now,
                    Rohith.G

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 5 Mar 2016, 11:01 last edited by
                      #10

                      sorry, im not sure i understand

                      z = new QTableWidgetItem("z");
                      z->setCheckState(Qt::Unchecked);

                      z is just a variable name of type QTableWidgetItem *
                      "z" is the Text for the QTableWidgetItem .

                      table->item(i, j); do not care how u inserted the items.
                      This would do the same

                      QTableWidgetItem *MyItem = new QTableWidgetItem("z");
                      MyItem->setCheckState(Qt::Unchecked);

                      and table->item(i, j) would still work.

                      R 1 Reply Last reply 5 Mar 2016, 11:08
                      1
                      • M mrjj
                        5 Mar 2016, 11:01

                        sorry, im not sure i understand

                        z = new QTableWidgetItem("z");
                        z->setCheckState(Qt::Unchecked);

                        z is just a variable name of type QTableWidgetItem *
                        "z" is the Text for the QTableWidgetItem .

                        table->item(i, j); do not care how u inserted the items.
                        This would do the same

                        QTableWidgetItem *MyItem = new QTableWidgetItem("z");
                        MyItem->setCheckState(Qt::Unchecked);

                        and table->item(i, j) would still work.

                        R Offline
                        R Offline
                        Rohith
                        wrote on 5 Mar 2016, 11:08 last edited by
                        #11

                        @mrjj

                        Sorry for my wrong assumption the Code working fine in any condition and thanks a lot for your help and answering my doubts patiently thankyou very much for you help

                        Rohith.G

                        M 1 Reply Last reply 5 Mar 2016, 11:34
                        1
                        • R Rohith
                          5 Mar 2016, 11:08

                          @mrjj

                          Sorry for my wrong assumption the Code working fine in any condition and thanks a lot for your help and answering my doubts patiently thankyou very much for you help

                          Rohith.G

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 5 Mar 2016, 11:34 last edited by
                          #12

                          @Rohith

                          np. sometimes its hard to understand the real issue
                          when seeing just part of code but
                          glad it worked :)

                          you are very welcome.

                          1 Reply Last reply
                          0

                          1/12

                          5 Mar 2016, 09:37

                          • Login

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