PySide switching widgets with events?

前端 未结 2 903
时光说笑
时光说笑 2021-01-26 19:16

I can\'t figure it. I want to be able to swap out QWidgets according to events like button clicks but I am missing something and I haven\'t been able to search out any example a

2条回答
  •  佛祖请我去吃肉
    2021-01-26 19:43

    You have to use QStackedWidget, where you update the indexes.

    class Example(QtGui.QWidget):
        def __init__(self, *args, **kwargs):
            super(Example, self).__init__(*args, **kwargs)
            self.initUI()
    
        def initUI(self):
            hbox = QtGui.QHBoxLayout(self)
            self.stacked = QtGui.QStackedWidget(self)
            self.stacked.addWidget(QtGui.QCalendarWidget())
            self.stacked.addWidget(QtGui.QTextEdit())
    
            splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
    
            for text in ["Calendar", "TextEdit"]:
                btn = QtGui.QPushButton(text, self)
                btn.clicked.connect(self.setWidget)
                splitter1.addWidget(btn)
    
            splitter1.addWidget(self.stacked)
            hbox.addWidget(splitter1)
            self.setLayout(hbox)
            self.setGeometry(0, 0, 600, 600)
            self.setWindowTitle('Switching QWidgets')
            self.show()
    
        def setWidget(self):
            source = self.sender()
            if source.text() == "Calendar":        
                self.stacked.setCurrentIndex(0)
            elif source.text() == "TextEdit":
                self.stacked.setCurrentIndex(1)   
    

提交回复
热议问题