【发布时间】:2019-12-04 17:44:56
【问题描述】:
我想根据两个 QComboBox(es) 的选定文本启用或禁用 QDialogButtonBox,或者最好只启用 QDialog 中的 OK 按钮。
我的例子如下。它目前不工作,并且在启用或禁用 QDialogButtonBox 时,两个 ComboBox 相互独立工作。
import sys
from PyQt5.QtCore import QSignalMapper, pyqtSlot
from PyQt5.QtWidgets import (QGroupBox, QFormLayout, QLabel, QComboBox,
QApplication, QDialog, QDialogButtonBox,
QVBoxLayout)
class SheetColumns(QDialog):
def __init__(self, column_header):
super().__init__()
self.setMinimumWidth(300)
self.setWindowTitle("Input Column Names")
self.column_headers = column_header
self.column_headers.insert(0, ' ')
self.setWhatsThis('Please match columns in your data sheet names'
' with the right side labels')
col_names = ["Student Name:", "Student ID:", "Cohort:", "Gender:",
"College:", "Department:", "Major:", "Minor", "Email:",
"Adviser", "Adviser Email"]
self.form_group_box = QGroupBox("Specify Column Names")
self.layout = QFormLayout()
for col_name in col_names:
combo = QComboBox()
combo.addItems(self.column_headers)
self.layout.addRow(QLabel(col_name), combo)
self.form_group_box.setLayout(self.layout)
self.button_box = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.button_box.setEnabled(False)
self.layout.itemAt(0, 1).widget().currentTextChanged.connect(
self.check_validity)
self.layout.itemAt(1, 1).widget().currentTextChanged.connect(
self.check_validity)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
main_layout = QVBoxLayout()
main_layout.addWidget(self.form_group_box)
main_layout.addWidget(self.button_box)
self.setLayout(main_layout)
def check_validity(self, text):
print(text)
if text == ' ':
self.button_box.setEnabled(False)
else:
self.button_box.setEnabled(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = SheetColumns(['name student', 'id', 'cohort', 'test 1'])
sys.exit(dialog.exec_())
我希望当两个 ComboBox 中的 currentText(s) 不是 ' ' 时启用 QDialogButtonBox,而当它们都是 ' ' 时它被禁用。
我尝试使用QSignalMapper。
但是,我无法让它工作。
class SheetColumns(QDialog):
def __init__(self, column_header):
super().__init__()
self.setMinimumWidth(300)
self.setWindowTitle("Input Column Names")
self.column_headers = column_header
self.column_headers.insert(0, ' ')
self.setWhatsThis('Please match columns in your data sheet names'
' with the right side labels')
col_names = ["Student Name:", "Student ID:", "Cohort:", "Gender:",
"College:", "Department:", "Major:", "Minor", "Email:",
"Adviser", "Adviser Email"]
self.form_group_box = QGroupBox("Specify Column Names")
self.layout = QFormLayout()
for col_name in col_names:
combo = QComboBox()
combo.addItems(self.column_headers)
self.layout.addRow(QLabel(col_name), combo)
self.form_group_box.setLayout(self.layout)
self.button_box = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.button_box.setEnabled(False)
self.mapper = QSignalMapper(self)
comb_bx1 = self.layout.itemAt(0, 1).widget()
comb_bx2 = self.layout.itemAt(1, 1).widget()
comb_bx1.currentTextChanged.connect(self.mapper.map)
comb_bx2.currentTextChanged.connect(self.mapper.map)
self.mapper.setMapping(comb_bx1, comb_bx1.currentText())
self.mapper.setMapping(comb_bx2, comb_bx2.currentText())
self.mapper.mapped.connect(self.check_validity)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
main_layout = QVBoxLayout()
main_layout.addWidget(self.form_group_box)
main_layout.addWidget(self.button_box)
self.setLayout(main_layout)
def check_validity(self, text):
print(text)
if text == ' ':
self.button_box.setEnabled(False)
else:
self.button_box.setEnabled(True)
谁能告诉我我做错了什么,或者有更好的方法吗?
提前致谢
【问题讨论】: