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. Get content of cell from QTableView
Forum Updated to NodeBB v4.3 + New Features

Get content of cell from QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
88 Posts 7 Posters 59.0k 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.
  • I imene

    @SGaist said in Get content of cell from QTableView:

    ui->tableView->model()->index(index.row(),0).data().toInt();

    It doesn't work for me it returns "member reference base char'(const char,int) throw()' is not a structure or union":
    e59279c3-7ca8-4fff-b9a6-06658e43721f-image.png

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #10

    @imene
    You are in the middle of MainWindow constructor. On the line you show, in the index.row() expression what is the index variable, where is it declared, what is its type?

    The point of this question is to get the content of a QTableView cell, where the cell you want is in the model at QModelIndex index.

    1 Reply Last reply
    1
    • Karoluss96K Offline
      Karoluss96K Offline
      Karoluss96
      wrote on last edited by
      #11

      Hi, I've tried the same (almost, because in python), but I gain error: name 'index' is not defined

      jsulmJ 1 Reply Last reply
      0
      • Karoluss96K Karoluss96

        Hi, I've tried the same (almost, because in python), but I gain error: name 'index' is not defined

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

        @Karoluss96 Please post the code.
        Did you define the "index" member variable?

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

        1 Reply Last reply
        0
        • Karoluss96K Offline
          Karoluss96K Offline
          Karoluss96
          wrote on last edited by
          #13

          No, but It is noe build-in function? like here: https://stackoverflow.com/questions/37035756/how-to-select-multiple-rows-in-qtableview-using-selectionmodel

          jsulmJ 1 Reply Last reply
          0
          • Karoluss96K Karoluss96

            No, but It is noe build-in function? like here: https://stackoverflow.com/questions/37035756/how-to-select-multiple-rows-in-qtableview-using-selectionmodel

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

            @Karoluss96 If you read this thread, especially what @JonB asked you will see that it is unclear what index is in the code posted. Sinse it is not my code I also don't know.

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

            1 Reply Last reply
            0
            • Karoluss96K Offline
              Karoluss96K Offline
              Karoluss96
              wrote on last edited by
              #15

              I put tableView.currentIndex() here, that take the number of index for data, which work correct, but later .data() says: ' 'int' object has no attribute 'data''

              jsulmJ JonBJ 2 Replies Last reply
              0
              • Karoluss96K Karoluss96

                I put tableView.currentIndex() here, that take the number of index for data, which work correct, but later .data() says: ' 'int' object has no attribute 'data''

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

                @Karoluss96 Can you please post the code?

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

                1 Reply Last reply
                0
                • Karoluss96K Karoluss96

                  I put tableView.currentIndex() here, that take the number of index for data, which work correct, but later .data() says: ' 'int' object has no attribute 'data''

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #17

                  @Karoluss96 said in Get content of cell from QTableView:

                  but later .data() says: ' 'int' object has no attribute 'data''

                  Do you expect people to say anything useful about this when you show no code?

                  1 Reply Last reply
                  0
                  • Karoluss96K Offline
                    Karoluss96K Offline
                    Karoluss96
                    wrote on last edited by
                    #18

                    My plan is to get data from whole table and separate it between rows to make sql question (Insert Into those who are new added)
                    now (after cutting of .toInt() ) looks that:

                    data= self.dlg.tableView_3.model().index(self.dlg.tableView_3.currentIndex().row(),0)
                            dt=data.data()
                            print (dt)
                    
                    1 Reply Last reply
                    0
                    • Karoluss96K Offline
                      Karoluss96K Offline
                      Karoluss96
                      wrote on last edited by
                      #19

                      It take only the first record from selected row, wheras I need all data from selected row, which later I want to put (separatly one-by-one) to a sql question

                      JonBJ 1 Reply Last reply
                      0
                      • Karoluss96K Karoluss96

                        It take only the first record from selected row, wheras I need all data from selected row, which later I want to put (separatly one-by-one) to a sql question

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #20

                        @Karoluss96 said in Get content of cell from QTableView:

                        It take only the first record from selected row

                        Careful with words, you mean column not record. So look at your 0 argument which only fetches the data for column 0, and write code to pick up all the columns in the row if that is what you want....

                        1 Reply Last reply
                        0
                        • Karoluss96K Offline
                          Karoluss96K Offline
                          Karoluss96
                          wrote on last edited by
                          #21

                          Maybe some loop will be good?

                          JonBJ 1 Reply Last reply
                          0
                          • Karoluss96K Karoluss96

                            Maybe some loop will be good?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #22

                            @Karoluss96 I would think that would indeed be a good idea if you want to get the content of each column :)

                            1 Reply Last reply
                            0
                            • Karoluss96K Offline
                              Karoluss96K Offline
                              Karoluss96
                              wrote on last edited by
                              #23

                              Now I have:

                              data= self.dlg.tableView_3.model().index(self.dlg.tableView_3.selectionModel().currentIndex().row(),0)
                              dane=data.data()
                                     for i in data:
                                         data=self.model4.selectIndex(i)
                                         print (data)
                              

                              but it get error: 'QModelIndex' object is not iterable

                              JonBJ 1 Reply Last reply
                              0
                              • Karoluss96K Karoluss96

                                Now I have:

                                data= self.dlg.tableView_3.model().index(self.dlg.tableView_3.selectionModel().currentIndex().row(),0)
                                dane=data.data()
                                       for i in data:
                                           data=self.model4.selectIndex(i)
                                           print (data)
                                

                                but it get error: 'QModelIndex' object is not iterable

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #24

                                @Karoluss96
                                This is wrong. Your data variable is a single data index item, you can't go for i in data. Please look at the documentation for correct usage of QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const or QVariant QModelIndex::data(int role = Qt::DisplayRole) const

                                1 Reply Last reply
                                0
                                • Karoluss96K Offline
                                  Karoluss96K Offline
                                  Karoluss96
                                  wrote on last edited by
                                  #25

                                  hmm, so maybe the the whole row at begin in loop?

                                  JonBJ 1 Reply Last reply
                                  0
                                  • Karoluss96K Karoluss96

                                    hmm, so maybe the the whole row at begin in loop?

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #26

                                    @Karoluss96
                                    I really should not have to tell you this! If you want to allow for picking all the columns in a row even if just one cell is selected (so you can't only look at selected cells) you will want something like:

                                    row = self.dlg.tableView_3.selectionModel().currentIndex().row()
                                    model = self.dlg.tableView_3.model()
                                    for col in range(model.columnCount()):
                                        print(model.index(row, col).data())
                                    # Or I think you can do this in Python:
                                    rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
                                    print(rowData)
                                    
                                    1 Reply Last reply
                                    1
                                    • Karoluss96K Offline
                                      Karoluss96K Offline
                                      Karoluss96
                                      wrote on last edited by
                                      #27

                                      @JonB said in Get content of cell from QTableView:

                                      self.dlg.tableView_3.selectionModel().currentIndex().row()

                                      Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.

                                      Thnaks for help!

                                      JonBJ 1 Reply Last reply
                                      0
                                      • Karoluss96K Karoluss96

                                        @JonB said in Get content of cell from QTableView:

                                        self.dlg.tableView_3.selectionModel().currentIndex().row()

                                        Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.

                                        Thnaks for help!

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by
                                        #28

                                        @Karoluss96
                                        I don't know why you quote the first line of this code, but anyway.

                                        Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.

                                        And what is the problem with that? Assuming

                                        rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
                                        

                                        works in Python you have rowData containing all the values you need for your SQL statement.

                                        1 Reply Last reply
                                        1
                                        • Karoluss96K Offline
                                          Karoluss96K Offline
                                          Karoluss96
                                          wrote on last edited by
                                          #29

                                          I suppose true. I forgot to change the first word in my previous answer from nagative to positive :-)

                                          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