QPushButton doesn't do anything after clicking

440 Views Asked by At

I am a beginner with Qt and PySide with Python and I am facing a problem I don't know how to solve. I am working on FreeCAD and I try to design a simple window with push buttons but I can't make them work. When I click on them, their text should change but nothing happen. Here is a piece of my code:

class ManageDialog:
    def __init__(self, path):
        self.form = FreeCADGui.PySideUic.loadUi(path)
        self.form.setWindowTitle("Linked files manager")
       
        QListWidgetItem("First item", self.form.listWidget)
        QListWidgetItem("Second item", self.form.listWidget)

        self.form.RemoveButton.clicked.connect(self.remove)

        self.form.show()
    
    def remove(self):
        self.form.RemoveButton.setText("File removed")

path_to_ui = FreeCAD.getHomePath() + "/Mod/ThesisExistingBridges/ManageDialog.ui"
w = ManageDialog(path_to_ui)

My window is created according to a UI file done with Qt Creator. The design of the window is correct also and I can add items to a list widget for example but the buttons are not activated when clicked.

Here is a sample of UI file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>494</width>
    <height>459</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QListWidget" name="listWidget">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>471</width>
     <height>401</height>
    </rect>
   </property>
  </widget>
  <widget class="QWidget" name="horizontalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>420</y>
     <width>195</width>
     <height>31</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QPushButton" name="RemoveButton">
      <property name="text">
       <string>Remove</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="ReloadFromButton">
      <property name="text">
       <string>Reload from</string>
      </property>
      <property name="autoDefault">
       <bool>false</bool>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QPushButton" name="OKButton">
   <property name="geometry">
    <rect>
     <x>390</x>
     <y>420</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>OK</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
1

There are 1 best solutions below

0
On

I make it work in changing the method remove() in @static mode.

class ManageDialog:
        def __init__(self, path):
            self.form = FreeCADGui.PySideUic.loadUi(path)
            self.form.setWindowTitle("Linked files manager")
                    
            QListWidgetItem("First item", self.form.listWidget)
            QListWidgetItem("Second item", self.form.listWidget)
        
            def clickedremove():
                self.remove(self.form)
                    
            self.form.RemoveButton.clicked.connect(clickedremove)
        
            self.form.show()
            
        @staticmethod
        def remove(f):
            f.RemoveButton.setText("File removed")
    
path_to_ui = FreeCAD.getHomePath() + "/Mod/ThesisExistingBridges/ManageDialog.ui"
w = ManageDialog(path_to_ui)

It is maybe not the smartest way but it's working.