我有幾個問題。從Pyside的子窗口獲取設置
如何正確得到子窗口設置當我按下「打印」按鈕?
如何關閉設置窗口,並保存/提交更改,只有當用戶按下「OK」 VS「取消」剛剛關閉對話框,並駁回了變化。
設置窗口
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_())
約當用戶點擊取消就OK了什麼。當我打開對話框和設置是用戶最後選擇的 – JokerMartini
當前,設置窗口值將在您每次打開窗口時重置。如果你想讓它們代表* current *設置,你需要修改設置窗口來接受參數,這樣你就可以傳入* current *設置 - SettingsWindow(self,settings = {'min':4,'max ':10,'count':50})'然後使用這些設置來設置GUI元素的值 –