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

Get content of cell from QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
88 Posts 7 Posters 53.6k 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.
  • K Offline
    K Offline
    Karoluss96
    wrote on 12 Sept 2022, 08:31 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

    J 1 Reply Last reply 12 Sept 2022, 08:37
    0
    • K Karoluss96
      12 Sept 2022, 08:31

      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

      J Offline
      J Offline
      JonB
      wrote on 12 Sept 2022, 08:37 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
      • K Offline
        K Offline
        Karoluss96
        wrote on 12 Sept 2022, 08:43 last edited by
        #21

        Maybe some loop will be good?

        J 1 Reply Last reply 12 Sept 2022, 08:46
        0
        • K Karoluss96
          12 Sept 2022, 08:43

          Maybe some loop will be good?

          J Offline
          J Offline
          JonB
          wrote on 12 Sept 2022, 08:46 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
          • K Offline
            K Offline
            Karoluss96
            wrote on 12 Sept 2022, 08:56 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

            J 1 Reply Last reply 12 Sept 2022, 09:04
            0
            • K Karoluss96
              12 Sept 2022, 08:56

              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

              J Offline
              J Offline
              JonB
              wrote on 12 Sept 2022, 09:04 last edited by JonB 9 Dec 2022, 09:36
              #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
              • K Offline
                K Offline
                Karoluss96
                wrote on 12 Sept 2022, 09:12 last edited by
                #25

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

                J 1 Reply Last reply 12 Sept 2022, 09:34
                0
                • K Karoluss96
                  12 Sept 2022, 09:12

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 12 Sept 2022, 09:34 last edited by JonB 9 Dec 2022, 09:39
                  #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
                  • K Offline
                    K Offline
                    Karoluss96
                    wrote on 12 Sept 2022, 10:35 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!

                    J 1 Reply Last reply 12 Sept 2022, 10:40
                    0
                    • K Karoluss96
                      12 Sept 2022, 10:35

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

                      J Offline
                      J Offline
                      JonB
                      wrote on 12 Sept 2022, 10:40 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
                      • K Offline
                        K Offline
                        Karoluss96
                        wrote on 12 Sept 2022, 10:55 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
                        • K Offline
                          K Offline
                          Karoluss96
                          wrote on 12 Sept 2022, 12:49 last edited by
                          #30

                          If can add something for it:
                          I need to change all empty (if is empty!) records in rowData from 'None' Type to 'Null' (for better recognising it in SQL Question).

                          It's obviosly rather a python than qt question, but I can't find good answer in a differnt forum.

                          J 1 Reply Last reply 12 Sept 2022, 12:59
                          0
                          • K Karoluss96
                            12 Sept 2022, 12:49

                            If can add something for it:
                            I need to change all empty (if is empty!) records in rowData from 'None' Type to 'Null' (for better recognising it in SQL Question).

                            It's obviosly rather a python than qt question, but I can't find good answer in a differnt forum.

                            J Offline
                            J Offline
                            JonB
                            wrote on 12 Sept 2022, 12:59 last edited by
                            #31

                            @Karoluss96
                            Sorry but this question does not mean much. If you are, say, generating an INSERT statement from the column values in a row, and some of these are Python None, and you need to pass NULL for this to SQL in VALUES clause, then you must write code to do so at the appropriate point. If you use, say, QSqlQuery to generate the statement you may find that QSqlQuery::addBindValue() does this for you if the value is Python None, I don't know.

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              Karoluss96
                              wrote on 12 Sept 2022, 13:12 last edited by
                              #32

                              @JonB said in Get content of cell from QTableView:

                              addBindValue()

                              well first I want to concatenate data from rowData into one 'train' of later sql Values like below:

                              dn2 = iden + ',\'' + teryt + '\',\'' +....+ DatMod
                                           
                              

                              where iden is rowCount[0], teryt is rowCount[1] etc.

                              but it take erorr:

                              TypeError: can only concatenate str (not "NoneType") to str

                              J J 2 Replies Last reply 12 Sept 2022, 13:14
                              0
                              • K Karoluss96
                                12 Sept 2022, 13:12

                                @JonB said in Get content of cell from QTableView:

                                addBindValue()

                                well first I want to concatenate data from rowData into one 'train' of later sql Values like below:

                                dn2 = iden + ',\'' + teryt + '\',\'' +....+ DatMod
                                             
                                

                                where iden is rowCount[0], teryt is rowCount[1] etc.

                                but it take erorr:

                                TypeError: can only concatenate str (not "NoneType") to str

                                J Online
                                J Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on 12 Sept 2022, 13:14 last edited by
                                #33

                                @Karoluss96 said in Get content of cell from QTableView:

                                TypeError: can only concatenate str (not "NoneType") to str

                                @JonB told you that you will have to write code for that. You need to check whether it is None, if it is add "NULL" instead of the value into the string.

                                But it would be way better to use QSqlQuery::addBindValue() anyway...

                                K 1 Reply Last reply 12 Sept 2022, 13:30
                                1
                                • K Karoluss96
                                  12 Sept 2022, 13:12

                                  @JonB said in Get content of cell from QTableView:

                                  addBindValue()

                                  well first I want to concatenate data from rowData into one 'train' of later sql Values like below:

                                  dn2 = iden + ',\'' + teryt + '\',\'' +....+ DatMod
                                               
                                  

                                  where iden is rowCount[0], teryt is rowCount[1] etc.

                                  but it take erorr:

                                  TypeError: can only concatenate str (not "NoneType") to str

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 12 Sept 2022, 13:27 last edited by
                                  #34

                                  @Karoluss96
                                  Both of what @jsulm said.

                                  Since I already wrote

                                  and some of these are Python None, and you need to pass NULL for this to SQL in VALUES clause, then you must write code to do so at the appropriate point.

                                  I don't understand why you come back with TypeError: can only concatenate str (not "NoneType") to str error message when you haven't done anything about it. Please read the answers and act upon them, else it's pointless asking/responding.

                                  1 Reply Last reply
                                  0
                                  • J jsulm
                                    12 Sept 2022, 13:14

                                    @Karoluss96 said in Get content of cell from QTableView:

                                    TypeError: can only concatenate str (not "NoneType") to str

                                    @JonB told you that you will have to write code for that. You need to check whether it is None, if it is add "NULL" instead of the value into the string.

                                    But it would be way better to use QSqlQuery::addBindValue() anyway...

                                    K Offline
                                    K Offline
                                    Karoluss96
                                    wrote on 12 Sept 2022, 13:30 last edited by Karoluss96 9 Dec 2022, 13:43
                                    #35

                                    @jsulm I try inspiring from here: https://cpp.hotexamples.com/examples/-/QSqlQuery/addBindValue/cpp-qsqlquery-addbindvalue-method-examples.html but still I have only one

                                    J 1 Reply Last reply 12 Sept 2022, 13:49
                                    0
                                    • K Karoluss96
                                      12 Sept 2022, 13:30

                                      @jsulm I try inspiring from here: https://cpp.hotexamples.com/examples/-/QSqlQuery/addBindValue/cpp-qsqlquery-addbindvalue-method-examples.html but still I have only one

                                      J Online
                                      J Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 12 Sept 2022, 13:49 last edited by
                                      #36

                                      @Karoluss96 said in Get content of cell from QTableView:

                                      but still I have only one

                                      Only one what? Can you please be more precise?

                                      K 1 Reply Last reply 12 Sept 2022, 14:13
                                      0
                                      • J jsulm
                                        12 Sept 2022, 13:49

                                        @Karoluss96 said in Get content of cell from QTableView:

                                        but still I have only one

                                        Only one what? Can you please be more precise?

                                        K Offline
                                        K Offline
                                        Karoluss96
                                        wrote on 12 Sept 2022, 14:13 last edited by
                                        #37

                                        @jsulm I mean if I try:

                                         a=query.addBindValue(rowData)
                                                    print (a)
                                        

                                        still have one 'None'

                                        J 2 Replies Last reply 12 Sept 2022, 14:15
                                        0
                                        • K Karoluss96
                                          12 Sept 2022, 14:13

                                          @jsulm I mean if I try:

                                           a=query.addBindValue(rowData)
                                                      print (a)
                                          

                                          still have one 'None'

                                          J Online
                                          J Online
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on 12 Sept 2022, 14:15 last edited by
                                          #38
                                          This post is deleted!
                                          1 Reply Last reply
                                          0

                                          28/88

                                          12 Sept 2022, 10:40

                                          • Login

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