2017-02-07 83 views
1

我有幾個問題。從Pyside的子窗口獲取設置

  1. 如何正確得到子窗口設置當我按下「打印」按鈕?

  2. 如何關閉設置窗口,並保存/提交更改,只有當用戶按下「OK」 VS「取消」剛剛關閉對話框,並駁回了變化。

enter image description here

設置窗口

import sys 
from PySide import QtGui, QtCore 


class SettingsWindow(QtGui.QDialog): 
    def __init__(self, parent=None): 
     super(SettingsWindow, self).__init__(parent) 
     self.resize(200, 150) 
     self.setWindowTitle('Settings') 
     self.initUI() 

    def initUI(self): 

     lb_max = QtGui.QLabel('Max') 
     self.ui_max = QtGui.QSpinBox() 
     self.ui_max.setValue(5) 

     lb_min = QtGui.QLabel('Min') 
     self.ui_min = QtGui.QSpinBox() 
     self.ui_min.setValue(10) 

     lb_count = QtGui.QLabel('Count') 
     self.ui_count = QtGui.QSpinBox() 
     self.ui_count.setValue(25) 

     self.buttons = QtGui.QDialogButtonBox(); 
     self.buttons.setOrientation(QtCore.Qt.Horizontal) 
     self.buttons.setStandardButtons(QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Cancel) 
     self.buttons.layout().setDirection(QtGui.QBoxLayout.LeftToRight) 

     grid = QtGui.QGridLayout() 
     grid.setContentsMargins(10,10,10,10) 
     grid.addWidget(lb_max,0,0) 
     grid.addWidget(self.ui_max,0,1) 
     grid.addWidget(lb_min,1,0) 
     grid.addWidget(self.ui_min,1,1) 
     grid.addWidget(lb_count,2,0) 
     grid.addWidget(self.ui_count,2,1) 
     grid.addWidget(self.buttons,3,1) 

     self.setLayout(grid) 

主窗口

class MainWindow(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     self.resize(200, 150) 
     self.setWindowTitle('Assets') 
     self.initUI() 

    def initUI(self): 

     self.mi_settings = QtGui.QAction('Settings', self) 
     self.mi_settings.triggered.connect(self.open_settings) 

     self.ui_button = QtGui.QPushButton('Print') 
     self.ui_button.clicked.connect(self.clicked_button) 

     menubar = self.menuBar() 
     fileMenu = menubar.addMenu('&File') 
     fileMenu.addAction(self.mi_settings) 

     grid = QtGui.QVBoxLayout() 
     grid.setContentsMargins(10,10,10,10) 
     grid.addWidget(self.ui_button) 

     main_widget = QtGui.QWidget() 
     main_widget.setLayout(grid) 
     self.setCentralWidget(main_widget) 

    def open_settings(self): 
     win = SettingsWindow() 
     win.exec_() 

    def clicked_button(self): 
     print 'Settings' 
     print '\tMax: ' 
     print '\tMin: ' 
     print '\tCount: ' 


if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    ex = MainWindow() 
    # ex = SettingsWindow() 
    ex.show() 
    sys.exit(app.exec_()) 

回答

1

首先,你需要的按鈕在對話框中連接起來,讓你可以告訴用戶是否取消與否:

class SettingsWindow(QtGui.QDialog): 
    ... 
    def initUI(self): 
     ... 
     self.buttons = QtGui.QDialogButtonBox() 
     ... 
     self.buttons.accepted.connect(self.accept) 
     self.buttons.rejected.connect(self.reject) 

其次,你應該想想你是如何設置的默認值在對話框中,你如何重置/檢索當前值。被用來做這將是有您存儲的值的中央設置字典,用對話框的方法之一,從用戶輸入更新:

class SettingsWindow(QtGui.QDialog): 
    ... 
    def getValues(self): 
     return { 
      'max': self.ui_max.value(), 
      'min': self.ui_min.value(), 
      'count': self.ui_count.value(), 
      } 

    def setValues(self, settings): 
     self.ui_max.setValue(settings['max']) 
     self.ui_min.setValue(settings['min']) 
     self.ui_count.setValue(settings['count']) 

class MainWindow(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     ... 
     # default values 
     self.settings = { 
      'max': 5, 
      'min': 10, 
      'count': 25, 
      } 

    def open_settings(self): 
     win = SettingsWindow() 
     # reset from current values 
     win.setValues(self.settings) 
     if win.exec_() == QtGui.QDialog.Accepted: 
      # update only if user clicked ok 
      self.settings.update(win.getValues()) 

    def clicked_button(self): 
     print 'Settings:' 
     for key in 'max', 'min', 'count': 
      print '\t%s = %s' % (key.title(), self.settings[key]) 

有許多方法來解決這樣的問題,但這應該給你一個總的想法。

0

您可以檢查exec_()命令的結果看對話是否被接受或拒絕。如果被接受,你可以閱讀GUI的值控制

win = SettingsWindow() 
r = win.exec_() 
if r: 
    min_val = win.ui_min.value() 
    max_val = win.ui_max.value() 
    cnt_val = win.ui_max.value() 
+0

約當用戶點擊取消就OK了什麼。當我打開對話框和設置是用戶最後選擇的 – JokerMartini

+0

當前,設置窗口值將在您每次打開窗口時重置。如果你想讓它們代表* current *設置,你需要修改設置窗口來接受參數,這樣你就可以傳入* current *設置 - SettingsWindow(self,settings = {'min':4,'max ':10,'count':50})'然後使用這些設置來設置GUI元素的值 –