gpt4 book ai didi

python - 从 Pyside 的子窗口获取设置

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:13 25 4
gpt4 key购买 nike

我有几个问题。

  1. 当我按“打印”按钮时,如何正确从子窗口获取设置?

  2. 如何关闭“设置”窗口并仅在用户按“确定”时保存/提交更改,而“取消”则仅关闭对话框并取消更改。

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

最佳答案

首先需要连接对话框中的按钮,以便判断用户是否取消了:

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])

解决此类问题的方法有很多种,但这应该能给您一个总体思路。

关于python - 从 Pyside 的子窗口获取设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42096783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com