如何按请求切换小部件可见性

时间:2017-01-26 00:47:14

标签: python pyqt qcombobox qdialog

下面的代码创建了一个包含三行小部件的单个对话框窗口: 一个combo,lineEdit和dateEdit。当combobox显示' Show LineEdit'我希望lineEdit可见并隐藏dateEdit。当"显示DateEdit"已被选中我想要隐藏LineEdit并显示DateEdit。如何实现呢?

enter image description here

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setLayout(QtGui.QVBoxLayout())

        for i in range(3):
            row = QtGui.QHBoxLayout()
            combo = QtGui.QComboBox()
            combo.addItems(['Show LineEdit','Show DateEdit'])
            combo.activated.connect(self.activated)
            row.addWidget(combo)

            self.lineEdit = QtGui.QLineEdit()
            self.dateEdit = QtGui.QDateEdit()
            self.dateEdit.setVisible(False)
            row.addWidget(self.lineEdit)
            row.addWidget(self.dateEdit)
            self.layout().insertLayout(i, row)

    def activated(self):
        print self.sender()

panel=Dialog()
panel.show()
app.exec_()

2 个答案:

答案 0 :(得分:1)

策略是创建一个字典,其中包含一个组合框作为键,并为相应的其他窗口小部件的字典值,然后使用可以返回文本或数字的激活方法。

from PyQt4 import QtGui

app = QtGui.QApplication([])


class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setLayout(QtGui.QVBoxLayout())

        self.widgets = {}

        for i in range(3):
            row = QtGui.QHBoxLayout()
            combo = QtGui.QComboBox()
            combo.addItems(['Show LineEdit', 'Show DateEdit'])
            combo.activated[str].connect(self.activated)
            row.addWidget(combo)

            lineEdit = QtGui.QLineEdit()
            dateEdit = QtGui.QDateEdit()

            self.widgets[combo] = [lineEdit, dateEdit]

            self.changeWidget(combo.currentText(), lineEdit, dateEdit)
            row.addWidget(lineEdit)
            row.addWidget(dateEdit)
            self.layout().insertLayout(i, row)

    def activated(self, text):
        linedit, dateEdit = self.widgets[self.sender()]
        self.changeWidget(text, linedit, dateEdit)

    def changeWidget(self, text, linedit, dateEdit):
        if text == 'Show LineEdit':
            linedit.setVisible(True)
            dateEdit.setVisible(False)
        elif text == 'Show DateEdit':
            linedit.setVisible(False)
            dateEdit.setVisible(True)


panel = Dialog()
panel.show()
app.exec_()

答案 1 :(得分:0)

受到上面发布的eyllanesc答案的启发,但没有字典。

from PyQt4 import QtGui
app = QtGui.QApplication([])

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setLayout(QtGui.QVBoxLayout())

        for i in range(3):
            row = QtGui.QHBoxLayout()
            combo = QtGui.QComboBox()
            combo.addItems(['Show LineEdit', 'Show DateEdit'])
            combo.activated[str].connect(self.activated)
            row.addWidget(combo)

            combo.lineEdit = QtGui.QLineEdit()
            combo.dateEdit = QtGui.QDateEdit()

            row.addWidget(combo.lineEdit)
            row.addWidget(combo.dateEdit)
            self.layout().insertLayout(i, row)

    def activated(self, title):
        combo = self.sender()
        if title == 'Show LineEdit':
            combo.lineEdit.setVisible(True)
            combo.dateEdit.setVisible(False)
        else:
            combo.lineEdit.setVisible(False)
            combo.dateEdit.setVisible(True)

panel = Dialog()
panel.show()
app.exec_()