Still confused when passing parameters to subclass/signal-slot
-
I am a newbie in Python/PyQt and I humbly ask for help.
I have a form where user can add a new record by clicking Add button and user can update the record by double clicking on the QTableView. The add and update call the same function "open_detail()" which then open another window "UnitDetail".
My problem is on the "update" -- I passed QModelIndex from the double click event. I can see row/column on the open_detail(). However, when I passed the index to UnitDetail it has None value.
Where is the logic error? Thank you.
My code snippet:
class Unit(QtWidgets.QWidget): def __init__(self): super(Unit, self).__init__() self.ui = Entity_Form() self.ui.setupUi(self) self.dtl = Unit_Form() self.ui.lblHeader.setText('Your Units') self.ui.btnAdd.setText('Add a Unit') self.ui.stackedWidget.setCurrentWidget(self.ui.pgList) self.ui.btnAdd.clicked.connect(self.open_detail) self.ui.tvTable.doubleClicked.connect(self.open_detail) self.model = QSqlRelationalTableModel(db=self.db) self.model.setTable("Unit") self.ui.tvTable.setModel(self.model) self.ui.tvTable.horizontalHeader().setStretchLastSection(True) self.model.select() def open_detail(self, index): self.index = index self.dtl = UnitDetail(self, index=None) self.dtl.show() class UnitDetail(Unit_Form, QtWidgets.QWidget): def __init__(self, parent, index): super().__init__() self.parent = parent self.setupUi(self) if index == None: self.btnDelete.hide() else: row=index.row() model=parent.model() print(f'row {row}')
-
@JuanW said in Still confused when passing parameters to subclass/signal-slot:
@jeremy_k,
Thank you so much, it works. I though the "None" in parameter index=None means to set the index value as None when I do not pass any value to it.Do that for the declaration rather than invocation.
EGclass Foo: def __init__(self, index = None): pass Foo() # index is None Foo(0) # index is 0
-
@JuanW said in Still confused when passing parameters to subclass/signal-slot:
I am a newbie in Python/PyQt and I humbly ask for help.
I have a form where user can add a new record by clicking Add button and user can update the record by double clicking on the QTableView. The add and update call the same function "open_detail()" which then open another window "UnitDetail".
My problem is on the "update" -- I passed QModelIndex from the double click event. I can see row/column on the open_detail(). However, when I passed the index to UnitDetail it has None value.
Where is the logic error? Thank you.
My code snippet:
class Unit(QtWidgets.QWidget): def open_detail(self, index): self.index = index self.dtl = UnitDetail(self, index=None) self.dtl.show()
Note the index = None in the UnitDetail constructor invocation.
-
@JuanW said in Still confused when passing parameters to subclass/signal-slot:
However, when I passed the index to UnitDetail it has None value.
@JuanW said in Still confused when passing parameters to subclass/signal-slot:
@jeremy_k ,
I did that because "add" button also call open_detail() with no index available. Any other clue?If you pass index = None, index will be None. I suspect that in open_details(), the intention is to invoke
UnitDetails(self, index)
, orUnitDetails(self, index=index)
. -
@JuanW said in Still confused when passing parameters to subclass/signal-slot:
@jeremy_k,
Thank you so much, it works. I though the "None" in parameter index=None means to set the index value as None when I do not pass any value to it.Do that for the declaration rather than invocation.
EGclass Foo: def __init__(self, index = None): pass Foo() # index is None Foo(0) # index is 0
-