Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 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.



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


  • Banned

    @Rishabh-Batra I am going to take a wild guess here (since you do not state it) that you have tied your GUI directly to your sqlite3 database which is an inherently bad approach for many reasons and is probably part of the reason for this issue.

    If you handle the GUI data within the GUI separate from the GUI data within the sqlite3 database the overall process is much cleaner and you do not affect your database without directly doing so.

    To help you understand this do a google search on MVC Methodology as this is what you should be implementing for a better overall design.

    Lastly if you include an MRE (Minimal Reproducible Example) we can better help you with your issue



  • @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.


  • Banned

    Okay then divide and conquer this. Create your GUI initially giving it a Dictionary of hard-coded data that represents the data coming from the database get your Gui to work with that data in the manner you are wanting. I do something similar with my project as I deal with a QTreeView (the QTreeWidget was not necessary) and QStandardItemModel supplying it a Dictionary of rows and it works like a charm

    Basically step back and simplify your approach making it overly complicated/complex just makes it harder to work with. I have a database class template that I shared with several folks already as well as the students in my free python-qt classroom on Discord but I think I also have an example program of rendering the QTreeView as well then all you would need to do is implement that add and substract aspects which are also fairly easily done if you work with your data behind the scenes and then just repopulate the view after that data has been adjusted


Log in to reply