Pyqt5-self.tablewidget.removeRow() removes every row in the table instead of only current row in qtablewidget
-
***#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
-
@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 aQTableView
instead of yourQTableWidget
with its "copied" data? That would provide support for inserting/deleting/updating rows in the view and database together. -
@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 . -
@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 -
@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.