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



  • 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


  • Qt Champions 2016

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



  • 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


  • Qt Champions 2016

    @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;
        }
    
    


  • 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."


  • Qt Champions 2016

    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?



  • 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

  • Qt Champions 2016

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



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


  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

    @Rohith

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

    you are very welcome.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.