PySide switching widgets with events?

冷暖自知 提交于 2021-02-05 08:50:57

问题


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 along the lines of the code below. What I want to do is click one of the top buttons and get the widget below to switch between either the QCalendar or QtextEdit. Where am I going wrong?

Thanks!

#!/usr/bin/python
import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        hbox = QtGui.QHBoxLayout(self)
        widget = QtGui.QCalendarWidget()
        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(widget)
        hbox.addWidget(splitter1)
        self.setLayout(hbox)
        self.setGeometry(0, 0, 600, 600)
        self.setWindowTitle('Switching QWidgets')
        self.show()

    def setWidget(self, pressed):

        source = self.sender()

        val1 = QtGui.QCalendarWidget()
        val2 = QtGui.QTextEdit()

        if source.text() == "Calendar":
            widget = val1
            QtGui.QWidget.update(Example.hbox)          
        elif source.text() == "TextEdit":
            widget = val2
            QtGui.QWidget.update(Example.hbox)       
        else:
            widget = val1
            QtGui.QWidget.update(Example.hbox)

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

回答1:


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)   



回答2:


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()


来源:https://stackoverflow.com/questions/47226833/pyside-switching-widgets-with-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!