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. Still confused when passing parameters to subclass/signal-slot
Forum Updated to NodeBB v4.3 + New Features

Still confused when passing parameters to subclass/signal-slot

Scheduled Pinned Locked Moved Solved Qt for Python
6 Posts 2 Posters 493 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.
  • J Offline
    J Offline
    JuanW
    wrote on last edited by
    #1

    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}')
    
    jeremy_kJ 2 Replies Last reply
    0
    • J JuanW

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

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by SGaist
      #6

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

      class Foo:
          def __init__(self, index = None): 
              pass
      
      Foo()  # index is None
      Foo(0) # index is 0
      

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      1
      • J JuanW

        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}')
        
        jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #2

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

        Asking a question about code? http://eel.is/iso-c++/testcase/

        J 1 Reply Last reply
        1
        • jeremy_kJ jeremy_k

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

          J Offline
          J Offline
          JuanW
          wrote on last edited by
          #3

          @jeremy_k ,
          I did that because "add" button also call open_detail() with no index available. Any other clue?

          1 Reply Last reply
          0
          • J JuanW

            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}')
            
            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #4

            @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), or UnitDetails(self, index=index).

            Asking a question about code? http://eel.is/iso-c++/testcase/

            J 1 Reply Last reply
            2
            • jeremy_kJ jeremy_k

              @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), or UnitDetails(self, index=index).

              J Offline
              J Offline
              JuanW
              wrote on last edited by
              #5

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

              jeremy_kJ 1 Reply Last reply
              0
              • J JuanW

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

                jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by SGaist
                #6

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

                class Foo:
                    def __init__(self, index = None): 
                        pass
                
                Foo()  # index is None
                Foo(0) # index is 0
                

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                1
                • J JuanW has marked this topic as solved on

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved