python-3.xindexingqtableviewpyside6

Pyside6, tableView.selectedIndexes, list index out of range


in this code function must take one index and simply delete it. And a problem is in delete_current_transaction, pycharm says that (index = self.ui.tableView.selectedIndexes()[0]) list index out of range.
Edit_current_transaction also must take only one category. But it don't gives this type of error

    def edit_current_transaction(self):
        index = self.ui.tableView.selectedIndexes()[0]
        id = str(self.ui.tableView.model().data(index))

        date = self.ui_window.dataEdit.text()
        category = self.ui_window.cb_chose_category.currentText()
        description = self.ui_window.le.description.text()
        balance = self.ui_window.le_balance.text()
        status = self.ui_window.cb_status.currentText()

        self.conn.update_transaction_query(date, category, description, balance, status, id)

        self.view_data()
        self.new_window.close()

    def delete_current_transaction(self):
        index = self.ui.tableView.selectedIndexes()[0]
        id = str(self.ui.tableView.model().data(index))

        self.conn.delete_transaction_query(id)

        self.view_data()
        self.new_window.close()

This function is a part of mainapp class

class MoneyManager(QMainWindow):
    def __init__(self):
        super(MoneyManager, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUI(self)
        self.conn = Data()
        self.view_data()

        self.ui.NewTransButton.clicked.connect(self.open_new_transaction_window)
        self.ui.EditTransButton.clicked.connect(self.open_new_transaction_window)
        self.ui.DeleteTransButton.clicked.connect(self.delete_current_transaction())

    def open_new_transaction_window(self):
        self.new_window = QtWidgets.QDialog()
        self.ui_window = Ui_New_Transaction_window()
        self.ui_window.setupUi(self.new_window)
        self.new_window.show()
        sender = self.sender()

        if sender.text() == "New transaction":
            self.ui_window.NewTransButton.clicked.connection(self.add_new_transaction)
        else:
            self.ui_window.EditTransButton.clicked.connection(self.edit_current_transaction)

    def delete_current_transaction(self):
        index = self.ui.tableView.selectedIndexes()[0]
        id = str(self.ui.tableView.model().data(index))

        self.conn.delete_transaction_query(id)

        self.view_data()
        self.new_window.close()

It takes data from Connection. function that gives this function data

def delete_transaction_query(self, id):
    sql_query = "DELETE FROM expenses WHERE ID=?"
    self.execute_query_with_params(sql_query, [id])

Solution

  • maybe the problem is that:

    class MoneyManager(QMainWindow):
        def __init__(self):
            //....other code 
            self.ui.DeleteTransButton.clicked.connect(self.delete_current_transaction())
    

    Remove the parentheses at the end of delete_current_transaction, we just want to pass the method, not call it.

    self.ui.DeleteTransButton.clicked.connect(self.delete_current_transaction)