【问题标题】:QPushButton doesn't do anything after clicking单击后 QPushButton 不执行任何操作
【发布时间】:2020-08-03 21:02:00
【问题描述】:

我是 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>

【问题讨论】:

    标签: python python-3.6 pyside2 qpushbutton freecad


    【解决方案1】:

    我让它在 @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)
    

    这可能不是最聪明的方法,但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 2012-04-16
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多