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. Solved::How Send All of QTableWidget to QList (foreach loop)
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 7.3k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        [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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            [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!!!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What do you mean by that ?

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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*.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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