Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Change the data in label by selected rows in Table View
Forum Updated to NodeBB v4.3 + New Features

Change the data in label by selected rows in Table View

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 2 Posters 476 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.
  • Karoluss96K Offline
    Karoluss96K Offline
    Karoluss96
    wrote on last edited by
    #1

    Good morning/afternoon everyone,

    I need a help in changing the text in label (ID of sql query's result) by selecting row from Table View in which ID doesn't presented. I've done that:

     dane = self.dlg.tableView_3.selectionModel().currentIndex()
     rzad = dane.row()#int row
     a=self.dlg.tableView_3.selectRow(rzad)
     self.dlg.label_67.setText(str(self.dlg.comboBox_9.currentText())) #powiat
     SqlId= 'SELECT ID FROM [tableName] where 1=1 and TERYT ='#get ID of table
     ter= ' \'' + self.dlg.comboBox_10.currentText() + '\''
     Sqlter = SqlId + ter  + ' ORDER BY ID DESC'
     query = QSqlQuery(db)
     query.exec_(Sqlter)
     while query.next(): 
         Iden = str(int(query.record().value('ID')))#get ID from query
         print (Iden)
         a1 = self.dlg.label_32.setText(Iden)
    

    but I works only if only record appears in table View.
    I need to find the way of changing the ID in labels if another row (for those where are more results in tableView) after clicking/selecting the row

    Have I put the screen of my app for better understanding of my problem?

    Thanks for every kind of help!

    JonBJ 1 Reply Last reply
    0
    • Karoluss96K Karoluss96

      Good morning/afternoon everyone,

      I need a help in changing the text in label (ID of sql query's result) by selecting row from Table View in which ID doesn't presented. I've done that:

       dane = self.dlg.tableView_3.selectionModel().currentIndex()
       rzad = dane.row()#int row
       a=self.dlg.tableView_3.selectRow(rzad)
       self.dlg.label_67.setText(str(self.dlg.comboBox_9.currentText())) #powiat
       SqlId= 'SELECT ID FROM [tableName] where 1=1 and TERYT ='#get ID of table
       ter= ' \'' + self.dlg.comboBox_10.currentText() + '\''
       Sqlter = SqlId + ter  + ' ORDER BY ID DESC'
       query = QSqlQuery(db)
       query.exec_(Sqlter)
       while query.next(): 
           Iden = str(int(query.record().value('ID')))#get ID from query
           print (Iden)
           a1 = self.dlg.label_32.setText(Iden)
      

      but I works only if only record appears in table View.
      I need to find the way of changing the ID in labels if another row (for those where are more results in tableView) after clicking/selecting the row

      Have I put the screen of my app for better understanding of my problem?

      Thanks for every kind of help!

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

      @Karoluss96
      I don't understand what you asking. You show code which picks up an ID from a combobox (don't know what is in it) and passes that to a SQL query to select just that one row and put the ID from that (which will be the same as the value from the combobox) into some label.

      What does " selecting row from Table View in which ID doesn't presented." and "another row (for those where are more results in tableView) after clicking/selecting the row" mean? If you don't have the desired ID available in the combobox's list (unless you allow the user to type in a value) then the user cannot pick it to query for that row.

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

        OK, I'll show it on screen, but please wait a few time

        1 Reply Last reply
        0
        • JonBJ JonB

          @Karoluss96
          I don't understand what you asking. You show code which picks up an ID from a combobox (don't know what is in it) and passes that to a SQL query to select just that one row and put the ID from that (which will be the same as the value from the combobox) into some label.

          What does " selecting row from Table View in which ID doesn't presented." and "another row (for those where are more results in tableView) after clicking/selecting the row" mean? If you don't have the desired ID available in the combobox's list (unless you allow the user to type in a value) then the user cannot pick it to query for that row.

          Karoluss96K Offline
          Karoluss96K Offline
          Karoluss96
          wrote on last edited by
          #4

          @JonB
          Przechwytywanie.PNG photo of my problem
          So in another way:

          1. After choosing parameters (like TERYT code) from textboxes and comboboxes on left, the result of sql are shown on the tableView below.
          2. The 1st record is immediately selected, and putting his values in adequates labes. The problem, about which I asked on this forum refers to ID of record, which isn't show in Table View below (my CEO decided so that), so I need to find another way to get and present ID of selected Record.

          My idea is to make sql again, but select only ID. It works, but only if there's one result record. If there're more records it shows it ID of the last one and don't change after select the different record (it still shows the ID of last record). I've tried to connect it with vertical header (in which the ID will be show), but it also doesn't work.

          Short version of question: How to change label's text by selecting row in table View (in which ID isn't presented).

          It that clear?

          JonBJ 1 Reply Last reply
          0
          • Karoluss96K Karoluss96

            @JonB
            Przechwytywanie.PNG photo of my problem
            So in another way:

            1. After choosing parameters (like TERYT code) from textboxes and comboboxes on left, the result of sql are shown on the tableView below.
            2. The 1st record is immediately selected, and putting his values in adequates labes. The problem, about which I asked on this forum refers to ID of record, which isn't show in Table View below (my CEO decided so that), so I need to find another way to get and present ID of selected Record.

            My idea is to make sql again, but select only ID. It works, but only if there's one result record. If there're more records it shows it ID of the last one and don't change after select the different record (it still shows the ID of last record). I've tried to connect it with vertical header (in which the ID will be show), but it also doesn't work.

            Short version of question: How to change label's text by selecting row in table View (in which ID isn't presented).

            It that clear?

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

            @Karoluss96 said in Change the data in label by selected rows in Table View:

            My idea is to make sql again, but select only ID. It works, but only if there's one result record. If there're more records it shows it ID of the last one and don't change after select the different record (it still shows the ID of last record). I've tried to connect it with vertical header (in which the ID will be show), but it also doesn't work.

            This is as unclear to me as your original question!

            Short version of question: How to change label's text by selecting row in table View (in which ID isn't presented).

            Well, you have to get the ID somehow from the row, else you don't know which one is selected!

            The problem, about which I asked on this forum refers to ID of record, which isn't show in Table View below (my CEO decided so that), so I need to find another way to get and present ID of selected Record.

            Right, so maybe this is the crux of the question! You choose not to show the ID for each row in the table, but you still need to access it, right? Then I can think of a few ways:

            • Do include fetching the ID in the model/table's SELECT query along with the columns you want to show. But set that column to be hidden/not visible in the QTableView. You can still access its value in the model from the QModelIndex selected in the view.

            • Do include ID as above, but interpose a QAbstractProxyModel between the SQL model and the view, where the proxy adjust columns to not pass the ID column through to the view. The proxy still lets you retrieve the ID column's value in a row from the source model.

            • If you have another unique column in the rows you could issue a query using that to obtain ID (or look through rows already in the model). But I don't think you are saying you have that anyway, plus it's messy.

            First two are preferable, were you only looking for the "hidden columns" feature per the first one?

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

              @JonB said in Change the data in label by selected rows in Table View:

              but interpose a QAbstractProxyModel between the SQL model and the view, where the proxy adjust columns to not pass the ID column through to the view. The proxy still lets you retrieve the ID column's value in a row from the source model.

              OK, I'll try like you suggest! The main problem is that, the data to tableView going through the QStandardItemModel Class not the QTabaleViewClass
              Example:

              self.model4.setItem(x, 0, QStandardItem(str(query.record().value(ier[1])))) 
              

              I can punt ID from query as:

              iden=int(query.record().value(ier[0]))
              

              ier is a table with the name of attributes [ID,TERYT,Area,sign,,,]

              JonBJ 1 Reply Last reply
              0
              • Karoluss96K Karoluss96

                @JonB said in Change the data in label by selected rows in Table View:

                but interpose a QAbstractProxyModel between the SQL model and the view, where the proxy adjust columns to not pass the ID column through to the view. The proxy still lets you retrieve the ID column's value in a row from the source model.

                OK, I'll try like you suggest! The main problem is that, the data to tableView going through the QStandardItemModel Class not the QTabaleViewClass
                Example:

                self.model4.setItem(x, 0, QStandardItem(str(query.record().value(ier[1])))) 
                

                I can punt ID from query as:

                iden=int(query.record().value(ier[0]))
                

                ier is a table with the name of attributes [ID,TERYT,Area,sign,,,]

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

                @Karoluss96 said in Change the data in label by selected rows in Table View:

                The main problem is that, the data to tableView going through the QStandardItemModel Class not the QTabaleViewClass

                Don't know what you mean, QStandardItemModel is a model, QTableViewClass is a view.

                Since you ultimately have some SQL database it is a "shame" if you are using a QStandardItemModel Class rather than some QSql... model class, as you will have extra work copying stuff around. You will need to include the ID from the query even in the QStandardItemModel wherever you populate it, so that it is accessible. But either hiding view columns or proxy-hiding a source model column can be applied just as much to a QStandardItemModel as to a SQL model, so the principle is the same.

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

                  OK, I wrote:
                  in function to show records in table view

                   a1=int(query.record().value(ier[0]))#take ID
                   self.model4.setItem(x, 32, QStandardItem(str(a1)))
                   self.dlg.tableView_3.setColumnHidden(32,True)#...and hide it!      
                  

                  in function to select records:

                  Iden = dane.siblingAtRow(rzad).siblingAtColumn(32).data()#Niewidoczny ID
                  self.dlg.label_32.setText(Iden)
                  

                  And it finally works!

                  Thanks for help!

                  1 Reply Last reply
                  1

                  • Login

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