【问题标题】:Save the selections made in the qcombobox in pyqt在pyqt中保存在qcombobox中所做的选择
【发布时间】:2014-09-30 13:38:23
【问题描述】:

我是 Pyqt 编程的新手。我尝试构建一个看起来像图片中的简单 GUI:

这是我写的代码:

from PyQt4 import QtCore, QtGui
import subprocess
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
     Ui_Dialog.hypermesh =0
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(435, 181)
        self.gridLayout_2 = QtGui.QGridLayout(Dialog)
        self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
        self.groupBox = QtGui.QGroupBox(Dialog)
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.gridLayout = QtGui.QGridLayout(self.groupBox)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.pushButton = QtGui.QPushButton(self.groupBox)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        self.comboBox = QtGui.QComboBox(self.groupBox)
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.gridLayout.addWidget(self.comboBox, 0, 1, 1, 1)
        self.pushButton_3 = QtGui.QPushButton(self.groupBox)
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.gridLayout.addWidget(self.pushButton_3, 1, 0, 1, 1)
        self.pushButton_2 = QtGui.QPushButton(self.groupBox)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.gridLayout.addWidget(self.pushButton_2, 1, 1, 1, 1)
        self.gridLayout_2.addWidget(self.groupBox, 0, 0, 1, 1)
        hypmv=[]
        cont=subprocess.Popen('ls /usr/local/bin/hm*',stdout=subprocess.PIPE,stdin=subprocess.PIPE,shell=True)
        contents = cont.stdout.readlines()
        for i in range(len(contents)):
            temp=contents[i].strip()
            temp=temp.split('/')
            size=len(temp)
            hypmv.append(temp[size-1])
            self.comboBox.addItem(_fromUtf8(""))
            self.comboBox.setItemText(i, _translate("Dialog", hypmv[i], None))
        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicked()")), Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")),self.hypm)
    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.groupBox.setTitle(_translate("Dialog", "Software Selector", None))
        self.pushButton.setText(_translate("Dialog", "Hypermesh(Pre processor)", None))
        self.pushButton_3.setText(_translate("Dialog", "Quit", None))
        self.pushButton_2.setText(_translate("Dialog", "Save", None))
    def hypm(self):
        Ui_Dialog.hypermesh  = unicode(self.comboBox.currentText())
        subprocess.Popen(Ui_Dialog.hypermesh,shell=True)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

在代码中,组合框项目在 gui 初始化时初始化,当我点击按钮时,应该打开当前在组合框中选择的软件版本。这个功能做的不错。

但现在我想保存用户所做的选择,这样每次他调用 gui 时,他不应该再次选择他之前在组合框中使用的版本。

因此,如果他第一次选择 hm_11.0,则应该每次都选择“hm_11.0”,直到他更改它为止。我该怎么做??

【问题讨论】:

    标签: python pyqt


    【解决方案1】:

    这是您要阅读的课程:http://pyqt.sourceforge.net/Docs/PyQt4/qsettings.html

    里面有一个小教程(ctrl+f 恢复 GUI 应用程序的状态)。

    您将使用 setValue 来存储与 par keyToSetting/valueOfSetting 相关的信息,并使用 value keyToSetting 来读取信息。

    示例: 导入系统 从 PyQt4 导入 QtGui,QtCore

    class Example(QtGui.QWidget):
    
        def __init__(self):
            super(Example, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            self.readSettings()
            self.setWindowTitle('Simple')      
    
            self.show()
    
        def readSettings(self):
            settings = QtCore.QSettings("AyaanTech", "SoftwareTest")
            self.setGeometry(settings.value("geometry", QtCore.QRect(300, 300, 250, 150)).toRect());
    
        def writeSettings(self):
            settings = QtCore.QSettings("AyaanTech", "SoftwareTest")
            settings.setValue("geometry", self.geometry());
    
        def closeEvent(self, event):
            quit_msg = "Do you want to save position and size?"
            reply = QtGui.QMessageBox.question(self, 'Message', 
                         quit_msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No, QtGui.QMessageBox.Cancel)
    
            if reply == QtGui.QMessageBox.Yes:
                self.writeSettings()
                event.accept()
            elif reply == QtGui.QMessageBox.No:
                event.accept()
            else:
                event.ignore()
    
    def main():
    
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()    
    

    【讨论】:

    • 我了解一点参考资料,如果你有@Elric,你可以举一些例子
    • 好的,创建了一个例子。告诉我你不明白的地方。我无法运行你的代码。
    猜你喜欢
    • 2013-12-26
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2011-02-10
    相关资源
    最近更新 更多