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 get access to row data in a QTableWidget
QtWS25 Last Chance

How to get access to row data in a QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 16.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.
  • G Offline
    G Offline
    Goldglv
    wrote on last edited by
    #1

    I have a comboBox where the user can select a team name.
    Bears
    Bengals
    Broncos

    I also have a hidden QTableWidget that contains 2 columns, first column is team name in like in comboBox, 2nd column is a position.

    Bears 1
    Bengals 2
    Broncos 3

    How do I get access to column 2 in QTableWidget? I will know the team name from the comboBox, if I use findItems, that just returns me a QList variable but I need to get column 2's values.

    Maybe using a QTableWidget isn't the way to go but how do I get access to column 2?

    Thank You.

    jsulmJ 1 Reply Last reply
    0
    • G Goldglv

      I have a comboBox where the user can select a team name.
      Bears
      Bengals
      Broncos

      I also have a hidden QTableWidget that contains 2 columns, first column is team name in like in comboBox, 2nd column is a position.

      Bears 1
      Bengals 2
      Broncos 3

      How do I get access to column 2 in QTableWidget? I will know the team name from the comboBox, if I use findItems, that just returns me a QList variable but I need to get column 2's values.

      Maybe using a QTableWidget isn't the way to go but how do I get access to column 2?

      Thank You.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Goldglv I'm not sure I understand your question. You can access cells using http://doc.qt.io/qt-5/qtablewidget.html#item

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      G 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Goldglv I'm not sure I understand your question. You can access cells using http://doc.qt.io/qt-5/qtablewidget.html#item

        G Offline
        G Offline
        Goldglv
        wrote on last edited by
        #3

        @jsulm but how do I know which row to access? Say for example user selected Bengals from combo, how do I know Bengals is row 2? I don't know of a find function to return me a row number.

        jsulmJ 1 Reply Last reply
        0
        • G Goldglv

          @jsulm but how do I know which row to access? Say for example user selected Bengals from combo, how do I know Bengals is row 2? I don't know of a find function to return me a row number.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Goldglv If you read the documentation you will find this: http://doc.qt.io/qt-5/qtablewidget.html#findItems
          It returns a QList<QTableWidgetItem>. If you read QTableWidgetItem documentation you will find http://doc.qt.io/qt-5/qtablewidgetitem.html#row

          If this is not appropriate you can still iterate over items of the column using http://doc.qt.io/qt-5/qtablewidget.html#item and check each item until you find the one you need.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5
            QVariant getSecondColumn(const QString& searchTerm) const{
            for (int i=0;i<tableWidget->model()->rowCount();++i){
            if(tableWidget->model()->index(i,0).data().toString().compare(searchTerm /*,Qt::CaseInsensitive*/)==0)
            return tableWidget->model()->index(i,1).data();
            }
            return QVariant()
            }
            

            My crusade against Q*Widget classes continues

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            G 1 Reply Last reply
            4
            • G Offline
              G Offline
              Goldglv
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • VRoninV VRonin
                QVariant getSecondColumn(const QString& searchTerm) const{
                for (int i=0;i<tableWidget->model()->rowCount();++i){
                if(tableWidget->model()->index(i,0).data().toString().compare(searchTerm /*,Qt::CaseInsensitive*/)==0)
                return tableWidget->model()->index(i,1).data();
                }
                return QVariant()
                }
                

                My crusade against Q*Widget classes continues

                G Offline
                G Offline
                Goldglv
                wrote on last edited by
                #7

                @VRonin Thank you for the example, this is very helpful. I'm still trying to learn the language....would you be able to show me an example using the #finditems and #row? Still having trouble figuring this out....

                reply

                jsulmJ 1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  Unfortunately I never use the QStandardItem interface but rather the QAbstractItemModel one (as above) so I can't be of much help with the findrow and row methods.

                  @Goldglv said in How to get access to row data in a QTableWidget:

                  Still having trouble figuring this out....

                  I you tell us what you are struggling with I'll try to help however I can

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • G Goldglv

                    @VRonin Thank you for the example, this is very helpful. I'm still trying to learn the language....would you be able to show me an example using the #finditems and #row? Still having trouble figuring this out....

                    reply

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Goldglv Did you read the documentation (I provided the links)?
                    First you call http://doc.qt.io/qt-5/qtablewidget.html#findItems to find the items you need.
                    It returns a list of QTableWidgetItem. A QTableWidgetItem has a method called row(): http://doc.qt.io/qt-5/qtablewidgetitem.html#row
                    It returns the row number in which the item is located.

                    QList<QTableWidgetItem *> items = ui->table->findItems("search string", Qt::MatchExactly);
                    for (QTableWidgetItem *item : items) {
                        int row = item->row();
                        // Do whatever you want to do
                    }
                    

                    Saying "Still having trouble figuring this out" is not very helpful as we do not know what the problems are.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    3

                    • Login

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