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
Forum Updated to NodeBB v4.3 + New Features

How to get access to row data in a QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 16.5k 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 17 Nov 2016, 03:49 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.

    J 1 Reply Last reply 17 Nov 2016, 05:30
    0
    • G Goldglv
      17 Nov 2016, 03:49

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Nov 2016, 05:30 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 17 Nov 2016, 05:39
      2
      • J jsulm
        17 Nov 2016, 05:30

        @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 17 Nov 2016, 05:39 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.

        J 1 Reply Last reply 17 Nov 2016, 05:45
        0
        • G Goldglv
          17 Nov 2016, 05:39

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 17 Nov 2016, 05:45 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
          • V Offline
            V Offline
            VRonin
            wrote on 17 Nov 2016, 11:54 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 21 Nov 2016, 14:55
            4
            • G Offline
              G Offline
              Goldglv
              wrote on 19 Nov 2016, 04:13 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • V VRonin
                17 Nov 2016, 11:54
                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 21 Nov 2016, 14:55 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

                J 1 Reply Last reply 22 Nov 2016, 05:37
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 21 Nov 2016, 15:40 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
                    21 Nov 2016, 14:55

                    @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

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 22 Nov 2016, 05:37 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

                    1/9

                    17 Nov 2016, 03:49

                    • Login

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