2014-09-30 108 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