0

我是 Qt 和 Python PySide 的初学者,我面临一个我不知道如何解决的问题。我正在使用 FreeCAD,我尝试设计一个带有按钮的简单窗口,但我无法让它们工作。当我点击它们时,它们的文本应该会改变,但什么也没有发生。这是我的一段代码:

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)

我的窗口是根据使用 Qt Creator 完成的 UI 文件创建的。窗口的设计也是正确的,例如,我可以将项目添加到列表小部件,但单击时按钮未激活。

这是一个 UI 文件示例:

<?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>
4

1 回答 1

0

我使它在@static 模式下更改方法 remove() 起作用。

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)

这可能不是最聪明的方式,但它正在发挥作用。

于 2020-08-03T13:02:00.210 回答