Looping through ctk.ctkCollapsibleButton widgets

Dear all,
I am trying to access ctk.ctkCollapsibleButton, I tried different things but nothing works. The goal is to creating/editing/removing objects during the loading function of the python plugin. Here is something I tried:

 class myPluginWidget(ScriptedLoadableModuleWidget):
          def setup(self):
                print("=======================================================")   
                print("  myPlugin Title     ")
                print("=======================================================")           
                ScriptedLoadableModuleWidget.setup(self)
                self.initMainPanel()

          def initMainPanel(self):
                print(" initMainPanel ")   

                self.mainClpsBtn = ctk.ctkCollapsibleButton()
                self.mainClpsBtn.text = "my Plugin Title"
                self.layout.addWidget(self.mainClpsBtn)
                self.mainFormLayout = qt.QFormLayout(self.mainClpsBtn)

                self.pEDt= qt.QLineEdit()            
                self.pEDt.setText("some Text")
                self.mainFormLayout.addRow(  self.pEDt )
              
                self.runBtn = qt.QPushButton("Run")
                self.mainFormLayout.addRow(self.runBtn)

                items = (self.mainFormLayout.itemAt(i) for i in range(self.mainFormLayout.count())) 
                print(items)
                for w in items:
                     print(w.widget())

The output is

        None
        None

I tried:

                     print(w)

The out put is:

       <generator object <genexpr> at 0x7fa27003f690>
       QLayoutItem (C++ Object 0x6877ee0)
       QLayoutItem (C++ Object 0x68f3270)

Same happened when I replaced self.mainFormLayout with self.layout. How can I get the type of the widget example QLineEdit or QPushButton and access their properties?

I don’t think PythonQt handles that level of runtime polymorphism - the w.widget() call should return a QWidget but it would need to be mapped back to the specific subtype. You could try with pure PythonQt and see if that’s a bug or a known limitation of the wrapping.

In any case it’s probably best to keep explicit python references to the widgets you want to manipulate, e.g. a list of QPushButton instances and then you can access them directly.

1 Like

Thanks for your reply. I will go for the list idea for now.

I would not recommend to delete any Qt widgets, as it is extremely difficult to do it correctly. You can hide any widgets that you don’t need (instead of deleting them); and show them if you need them again (instead of re-creating them).

1 Like

Thanks for the suggestion, I will consider this.