Solved::How Send All of QTableWidget to QList (foreach loop)



  • Hello Everyone
    i had a file and when open it with my program, i send every line of it to a row in QTableWidget
    after that, i want to check all of lines and for that, i need a foreach
    in this case, i have 5 columns and some rows and want to check all of rows in column 1 with an if statement.
    also need to keep other columns, so if, the if statement is true,* column 2 *of the row printed!
    (it works if use: @QList<QTableWidgetItem *item = ui->tableWidget->selectedItems@

    but i want works for every items, not just selected items!)
    i know its a little complicated, but you know what i saying.
    so, anyone knows how to do it?
    (i do it in c# and could use @foreach(listviewitem *item in mylist)@

    and with an if, it check everyline so easy.
    in qt, i have know idea how do it!)
    thanks everybody


  • Moderators

    Hi,

    You could iterate over the tablewidget using for loop, get the pointer to "item":http://qt-project.org/doc/qt-5/qtablewidget.html#item and add it to the list.



  • [quote author="p3c0" date="1414251749"]Hi,

    You could iterate over the tablewidget using for loop, get the pointer to "item":http://qt-project.org/doc/qt-5/qtablewidget.html#item and add it to the list.[/quote]

    thanks
    actually i used for loop, but i had to write to many lines and 2-3 for loop with 4-5 Qlist!
    so i want something easier and lighter.
    but if i couldn't use foreach loop, i had to use the old one


  • Lifetime Qt Champion

    Hi,

    Do you mean something like:

    @
    QList<QTableWidgetItem*> itemList;
    for (int row = 0 ; row < tableWidget->rowCount() ; ++row) {
    for (int col = 0 ; col < tableWidget->columnCount() ; ++col) {
    itemList << tableWidget->item(row, col);
    }
    }
    foreach (QTableWidgetItem * item, itemList) {
    // do something
    }
    @

    ?



  • [quote author="SGaist" date="1414271622"]Hi,

    Do you mean something like:

    @
    QList<QTableWidgetItem*> itemList;
    for (int row = 0 ; row < tableWidget->rowCount() ; ++row) {
    for (int col = 0 ; col < tableWidget->columnCount() ; ++col) {
    itemList << tableWidget->item(row, col);
    }
    }
    foreach (QTableWidgetItem * item, itemList) {
    // do something
    }
    @

    ?
    [/quote]

    yes, thank you
    its better. but still foreach has to works better than that in a IDE like QtCreator!!!


  • Lifetime Qt Champion

    What do you mean by that ?

    foreach is designed to work with data containers. A QTableWidget is not that kind of container



  • like i said before, i worked with c# and foreach in c# works with every kind of data.
    like the example i used in 1# post.
    but qt has some limits, like before using foreach we have send data to a Qlist and for me, this is a limit!


  • Lifetime Qt Champion

    c# foreach works on enumerable types. A QTableWidget doesn't fit that description, it's a widget on top of a model.

    The example you used is just a function that generates a QList of QTableWidgetItem*.


Log in to reply
 

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