Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Pyqt5-self.tablewidget.removeRow() removes every row in the table instead of only current row in qtablewidget
Forum Updated to NodeBB v4.3 + New Features

Pyqt5-self.tablewidget.removeRow() removes every row in the table instead of only current row in qtablewidget

Scheduled Pinned Locked Moved Unsolved Qt for Python
pythonqt for python
5 Posts 3 Posters 3.5k 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.
  • R Offline
    R Offline
    Rishabh Batra
    wrote on last edited by Rishabh Batra
    #1
    ***#displaying table***
    def tableDisplay(self):
         row = self.table.currentRow()
        query = "SELECT products, quantity, rate, total, cgst, sgst from items WHERE customer_id=? AND date=?"
    
    ***##on a button i am adding values in table from database***
        for i in reversed(range(self.table.rowCount())):
            self.table.removeRow(i)
        for row_data in ok:
            row_number = self.table.rowCount()
            self.table.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.table.setItem(row_number, column_number, QTableWidgetItem(str(data)))
        self.table.cellDoubleClicked.connect(self.doubleClicked)#double click event
    
    *********#on double click event i want to delete the row from table as well as database*********
    def doubleClicked(self):
        row = self.table.currentRow()
        query = "DELETE FROM items WHERE products = ? AND customer_id =? AND date = ? AND quantity =?"
        cur.execute(query, (item,customerId,date,qty))
        con.commit()
        self.table.removeRow(row)
     
    

    I think the problem is(in the last line) whenever i double click the table widget i want only current row to get removed but instaed every row in the table as well as database is removed. Any help will be appriciated

    JonBJ 1 Reply Last reply
    0
    • R Rishabh Batra
      ***#displaying table***
      def tableDisplay(self):
           row = self.table.currentRow()
          query = "SELECT products, quantity, rate, total, cgst, sgst from items WHERE customer_id=? AND date=?"
      
      ***##on a button i am adding values in table from database***
          for i in reversed(range(self.table.rowCount())):
              self.table.removeRow(i)
          for row_data in ok:
              row_number = self.table.rowCount()
              self.table.insertRow(row_number)
              for column_number, data in enumerate(row_data):
                  self.table.setItem(row_number, column_number, QTableWidgetItem(str(data)))
          self.table.cellDoubleClicked.connect(self.doubleClicked)#double click event
      
      *********#on double click event i want to delete the row from table as well as database*********
      def doubleClicked(self):
          row = self.table.currentRow()
          query = "DELETE FROM items WHERE products = ? AND customer_id =? AND date = ? AND quantity =?"
          cur.execute(query, (item,customerId,date,qty))
          con.commit()
          self.table.removeRow(row)
       
      

      I think the problem is(in the last line) whenever i double click the table widget i want only current row to get removed but instaed every row in the table as well as database is removed. Any help will be appriciated

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

      @Rishabh-Batra said in Pyqt5-self.tablewidget.removeRow() removes every row in the table instead of only current row in qtablewidget:

      instaed every row in the table as well as database is removed.

      Given your

      query = "DELETE FROM items WHERE products = ? AND customer_id =? AND date = ? AND quantity =?"

      I don't see how it is possible that could remove more than one row, so I don 't know. Verify it is only called once per double-click.

      Before you spend too much time on this. For what you seem to be doing (assuming you are SQL), have you considered using a QSqlTableModel together with a QTableView instead of your QTableWidget with its "copied" data? That would provide support for inserting/deleting/updating rows in the view and database together.

      R 1 Reply Last reply
      1
      • R Offline
        R Offline
        Rishabh Batra
        wrote on last edited by Rishabh Batra
        #3

        @JonB sir what i am doing is getting values from lineEdits and on a ADD button adding the values in sqlite3 database and simultaneously adding those values from database to QTableWidget.
        I think that after removing current row every row is becoming current row .

        1 Reply Last reply
        0
        • JonBJ JonB

          @Rishabh-Batra said in Pyqt5-self.tablewidget.removeRow() removes every row in the table instead of only current row in qtablewidget:

          instaed every row in the table as well as database is removed.

          Given your

          query = "DELETE FROM items WHERE products = ? AND customer_id =? AND date = ? AND quantity =?"

          I don't see how it is possible that could remove more than one row, so I don 't know. Verify it is only called once per double-click.

          Before you spend too much time on this. For what you seem to be doing (assuming you are SQL), have you considered using a QSqlTableModel together with a QTableView instead of your QTableWidget with its "copied" data? That would provide support for inserting/deleting/updating rows in the view and database together.

          R Offline
          R Offline
          Rishabh Batra
          wrote on last edited by
          #4

          @JonB sir what i am doing is getting values from lineEdits and on a ADD button adding the values in sqlite3 database and simultaneously adding those values from database to QTableWidget.
          I think that after removing current row every row is becoming current row

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Rishabh Batra
            wrote on last edited by
            #5

            @Denni-0 you are right sir I am completely doing it in wrong manner and no doubt I have to correct it.
            Actually I am developing a billing system. So how can we set a QTableWidget and the user select the values from Comobox and in the other columns it should calculate whatever mathematics formula we set on it. Afterwards we will store it in database. Also we should be able to add multiple rows.
            This will surely help me.

            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