Search code examples
pyqt4qwidget

how to change the visibility of overlapped widgets in pyqt based on signals


I have multiple TextBrowser boxes of same size overlapped and i need to display different TextBrowsers on different button clicks. Is there a way i could change the visibility of TextBrowser on different button clicks? Plz help me. Thanks.


Solution

  • I am not sure if I understand your question correctly, but does the following example help?

    #!/usr/bin/python
    #-*- coding:utf-8 -*-
    
    import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
    class changeVisibility(QWidget):    
        def __init__(self, parent=None):        
            super(changeVisibility, self).__init__(parent)
    
            self.textbrowserA = QTextBrowser()
            self.textbrowserA.setStyleSheet("background-color:red")
    
            self.textbrowserB = QTextBrowser()
            self.textbrowserB.setStyleSheet("background-color:blue")
    
    
            self.buttonA = QPushButton("Show A")
            self.buttonB = QPushButton("Show B")
    
            self.verticalLayout = QVBoxLayout(self)
    
            self.buttonA = QPushButton("Show A")
    
            self.verticalLayout.addWidget(self.textbrowserA)
            self.textbrowserA.show()
            self.verticalLayout.addWidget(self.textbrowserB)
            self.textbrowserB.hide()
    
            self.verticalLayout.addWidget(self.buttonA)
            self.verticalLayout.addWidget(self.buttonB)
    
            self.buttonA.clicked.connect(self.showA)
            self.buttonB.clicked.connect(self.showB)
    
        def showA(self):
            self.textbrowserB.hide()
            self.textbrowserA.show()
    
        def showB(self):
            self.textbrowserA.hide()
            self.textbrowserB.show()
    
    
    def main():
        app = QApplication(sys.argv)
        cV = changeVisibility()
        cV.show()
        app.exec_()
    
    
    if __name__ == '__main__':
        main()