使用PyQt中的QCheckBox或QComboBox更改小部件可见性

问题描述 投票:-1回答:2

我试图使用QLineEditQCheckBox显示/隐藏QComboBox(或其他一些小部件)。

python pyqt
2个回答
0
投票

您需要连接stateChanged信号(对于QCheckBox;每次检查/取消选中框时发出)或currentIndexChanged信号(对于QComboBox;每次在组合框中选择不同的项目时发出)连接到插槽(您也可以使用lambda在这里)。在那个插槽中你需要做的就是调用QLineEditshow()hide()方法来切换线编辑的可见性。


0
投票
from PyQt5 import Qt

class GUI(Qt.QWidget):     

    def __init__(self):
        super().__init__()

        layout = Qt.QVBoxLayout(self)        
        self.lineEdit = Qt.QLineEdit()
        self.lineEdit.setPlaceholderText("Hello Hossam Almasto")
        layout.addWidget(self.lineEdit)

        self.combo = Qt.QComboBox(self) #, activated = self.onChangeDir)
        self.combo.addItem("Test 1")
        self.combo.addItem("Test 2")
        layout.addWidget(self.combo)

        self.combo.activated[str].connect(self.onActivated)

    def onActivated(self, text):
        self.comboText = text        
        if self.comboText == "Test 2":
            self.lineEdit.hide()
        else:
            self.lineEdit.show()
            self.combo.setFocus()


if __name__ == '__main__':
    app = Qt.QApplication([])
    mw  = GUI()
    mw.show()
    app.exec()  
© www.soinside.com 2019 - 2024. All rights reserved.