简体   繁体   English

Python Inspect模块找不到pyqt类

[英]Python Inspect module does not find pyqt class

I have found a very nice solution here on this site in order to store gui settings in pyqt5: Python PyQt4 functions to save and restore UI widget values? 我在此站点上找到了一个非常好的解决方案,以便将gui设置存储在pyqt5中: Python PyQt4函数可用于保存和还原UI小部件值?

The solution is saved in the function guisave . 解决方案保存在guisave函数中。

Now I'm trying to implement this to my code. 现在,我正在尝试将其实现到我的代码中。 The idea is to close my gui with the exitAction button. 这个想法是用exitAction按钮关闭我的GUI。 This fires the closeApp function which fires the guisave function. 这将触发closeApp函数,而后者将触发guisave函数。 The guisave function should save now all my pyqt objects. guisave函数现在应该保存我所有的pyqt对象。
The problem is that this does not happen. 问题是这不会发生。 I'm not sure how I need to assign the ui variable in the guisave function. 我不确定如何在guisave函数中分配ui变量。 As you can see I tried to assign the mainwindow class. 如您所见,我尝试分配mainwindow类。 But this does not work. 但这是行不通的。 I'm also not sure if this works at all or if I need to scan all functions separately since the QEditLine are in the tab2UI function. 我也不确定这是否可行,或者我是否需要单独扫描所有功能,因为QEditLine位于tab2UI函数中。

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import inspect

class mainwindow(QMainWindow):
    def __init__(self, parent = None):
        super(mainwindow, self).__init__(parent)

        self.initUI()


    def initUI(self):
        exitAction = QAction(QIcon('icon\\exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)     
        exitAction.triggered.connect(self.closeApp)     

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.setMovable(False)
        self.toolbar.addAction(exitAction)

        self.tab_widget = QTabWidget(self)     # add tab

        self.tab2 = QWidget()
        self.tab_widget.addTab(self.tab2, "Tab_2") 
        self.tab2UI()

        self.setCentralWidget(self.tab_widget)


    def tab2UI(self):
        self.layout = QFormLayout()
        self.layout.addRow("Name",QLineEdit())
        self.layout.addRow("Address",QLineEdit())
        self.tab2.setLayout(self.layout)


    def closeApp(self):
        guisave()


def guisave():
    ui = mainwindow
    settings = QSettings('gui.ini', QSettings.IniFormat)

    for name, obj in inspect.getmembers(ui): 
        if isinstance(obj, QComboBox):
            name = obj.objectName()  # get combobox name
            index = obj.currentIndex()  # get current index from combobox
            text = obj.itemText(index)  # get the text for current index
            settings.setValue(name, text)  # save combobox selection to registry

        if isinstance(obj, QLineEdit):
            print obj.objectName()
            name = obj.objectName()
            value = obj.text()
            settings.setValue(name, value)  # save ui values, so they can be restored next time
            print name, value

        if isinstance(obj, QCheckBox):
            name = obj.objectName()
            state = obj.isChecked()
            settings.setValue(name, state)

        if isinstance(obj, QRadioButton):
            name = obj.objectName()
            value = obj.isChecked()  # get stored value from registry
            settings.setValue(name, value)


def main():
    app = QApplication(sys.argv)
    ex = mainwindow()
    ex.setGeometry(100,100,1000,600)
    ex.show()
    sys.exit(app.exec_())



if __name__ == '__main__':
    main()

The solution that I propose is responsible for saving the states of the QWidgets, but for this you must put a name through the property objectName : 我提出的解决方案是负责保存QWidgets的状态,但对于这一点,你必须通过属性把名字对象名

objectName : QString objectName:QString

This property holds the name of this object. 此属性保存此对象的名称。 You can find an object by name (and type) using findChild(). 您可以使用findChild()按名称(和类型)查找对象。 You can find a set of objects with findChildren(). 您可以使用findChildren()查找一组对象。

Access functions: 访问功能:

QString objectName() const QString objectName()常量

void setObjectName (const QString &name) void setObjectName (const QString&name)

So you should put a name, for example: 因此,您应该输入一个名称,例如:

self.tab_widget.setObjectName("tabWidget")

We can use the QApplication :: allWidgets () function to get all the widgets of the application, and then we get the properties and save them, the process of restoring is the reverse of the previous one. 我们可以使用QApplication :: allWidgets()函数获取应用程序的所有窗口小部件,然后获取属性并保存它们,还原过程与上一个过程相反。

def restore(settings):
    finfo = QFileInfo(settings.fileName())

    if finfo.exists() and finfo.isFile():
        for w in qApp.allWidgets():
            mo = w.metaObject()
            if w.objectName() != "":
                for i in range(mo.propertyCount()):
                    name = mo.property(i).name()
                    val = settings.value("{}/{}".format(w.objectName(), name), w.property(name))
                    w.setProperty(name, val)


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

The complete example is found here 完整的示例在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM