PySide切换小部件与事件?

时间:2017-11-10 16:05:35

标签: python pyside qwidget

我无法理解。我希望能够根据按钮点击等事件更换QWidgets,但我遗漏了一些东西,而且我还没有能够搜索下面代码中的任何示例。我想要做的是单击顶部按钮之一并获取下面的小部件,以在QCalendar或QtextEdit之间切换。我哪里错了?

谢谢!

#!/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()

2 个答案:

答案 0 :(得分:1)

您必须使用QStackedWidget来更新索引。

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)   

答案 1 :(得分:0)

如果您更改代码以保留对日历和textedit小部件的引用,则将它们都添加到init中的拆分器中...您可以在setWidget()中使用show和hide函数。

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