2014-09-30 41 views
1

我是Pyqt编程的新手。我试图建立一个简单的GUI,它看起来像一个在图片:在pyqt中保存qcombobox中的选择

enter image description here

这是我写的代码:

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_()) 

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

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

所以如果他第一次选择hm_11.0,它应该每次都是“hm_11.0”,直到他改变它为止。我怎样才能做到这一点??

回答

1

这是你想被阅读类:http://pyqt.sourceforge.net/Docs/PyQt4/qsettings.html

有像小教程中(Ctrl + F键还原GUI应用程序的状态)。

您将使用setValue通过par keyToSetting/valueOfSetting存储信息并使用值keyToSetting读取信息。

实施例: 进口SYS 从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()  
+0

我理解基准的点点ü可以举一些例子,如果u有@力克 – ayaan 2014-09-30 05:09:24

+0

确定,创建的示例。告诉我你不明白的东西。我无法运行你的代码。 – Elric 2014-09-30 05:39:09

+0

好吧,我会试试这个,告诉你@埃里克 – ayaan 2014-09-30 05:45:05