生成的QtWidget无法显示在还原窗口中

时间:2019-07-04 06:16:58

标签: python pyqt5 restore

我的程序生成的QPushButton存储在INI文件中,但是由于它没有在setupUi函数中初始化,因此我不希望在还原时调用它们。

    def setupUi(self,MainWindow):
      ...
       self.frame.mouseDoubleClickEvent = self.labelling
      ...

    def labelling(self,event):      
       if event.button()==QtCore.Qt.LeftButton:
       self.w = QPushButton(self.frame)
       self.w.setGeometry(QtCore.QRect(event.x(),event.y(), 25, 25))
       self.w.setObjectName(str(Ui_MainWindow.r))
       self.w.setText(str(Ui_MainWindow.r))
       self.w.show()


    ####################restore and save function#######################
    def restore(settings):
       finfo = QFileInfo(settings.fileName())
       if finfo.exists() and finfo.isFile():
       for m in qApp.allWidgets():
          mo = m.metaObject()
          if m.objectName() != "":
          for i in range(mo.propertyCount()):
                name = mo.property(i).name()
                val = settings.value("{}/{}".format(m.objectName(), 
                name), m.property(name))
                m.setProperty(name, val)
                m.show()


    def save(settings):
        for m in qApp.allWidgets():
        mo = m.metaObject()
        if m.objectName() != "":
        for i in range(mo.propertyCount()):
           name = mo.property(i).name()
           settings.setValue("{}/{}".format(m.objectName(), name), 
           m.property(name))

0 个答案:

没有答案