PySide switching widgets with events?

前端 未结 2 901
时光说笑
时光说笑 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)   
    
    0 讨论(0)
  • 2021-01-26 19:57

    If you change your code to keep a reference to the calendar and textedit widgets, then add them both to the splitter in init... You can just use the show and hide functions in your setWidget().

    class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
    
        self.initUI()
    
    def initUI(self):
    
        self.hbox = QtGui.QHBoxLayout(self)
        self.widget = QtGui.QCalendarWidget()
        self.widget2 = QtGui.QTextEdit()
        button1 = QtGui.QPushButton('Calendar', self)
        button1.setCheckable(True)
        button1.clicked[bool].connect(self.setWidget)
        button2 = QtGui.QPushButton('TextEdit', self)
        button2.setCheckable(True)
        button2.clicked[bool].connect(self.setWidget)
        splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
        splitter1.addWidget(button1)
        splitter1.addWidget(button2)
        splitter1.addWidget(self.widget)
        splitter1.addWidget(self.widget2)
        self.widget2.hide()
        self.hbox.addWidget(splitter1)
        self.setLayout(self.hbox)
        self.setGeometry(59, 59, 600, 600)
        self.setWindowTitle('Switching QWidgets')
        self.show()
    
    def setWidget(self, pressed):
    
        source = self.sender()
    
        if source.text() == "Calendar":
            self.widget.show()
            self.widget2.hide()
    
        elif source.text() == "TextEdit":
            self.widget.hide()
            self.widget2.show()
    
    0 讨论(0)
提交回复
热议问题