QStackedWidget实例

Posted exploer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QStackedWidget实例相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
# _*_ Coding: Utf-8 _*_

‘‘‘
    QStackedWidget
    useful properties and methods of qtabwidget
    funcs:
        addWidget(QWidget *widget), removeWidget(QWidget *widget) --- add & remove widget
        indexOf(QWidget *widget) --- get index of widget
        insertWidget(int index, QWidget *widget) --- insert widget to index
        count() --- return amount of widgets
        currentIndex() and currentWidget() --- return index and widget of current
    slots:
        setCurrentIndex(int index)
        setCurrentWidget(QWidget *widget)
    signals:
        currentChanged(int index)
        widgetRemoved(int index)
    --- edit by yangrui
‘‘‘
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import (QApplication, QWidget, QStackedWidget, QLabel, QComboBox)
from PyQt5.QtGui import *

class MystackedWidget(QWidget):
    qss = """
            #label1 {
                background: #545D4F;
            }

            #label2 {
                background: #FAA45D;
            }
            #label3 {
                background: #6AF45D;
            }
            #label4 {
                background: #2AA45D;
            }
        """
    def __init__(self):
        super(MystackedWidget, self).__init__()
        self.initUI()

    def initUI(self):
        self.resize(600, 400)
        self.stackedWidget = QStackedWidget(self)

        label1 = QLabel("page 1")
        label1.setFixedSize(150, 60)
        label1.setObjectName("label1")
        label2 = QLabel("page 2")
        label2.setFixedSize(150, 60)
        label2.setObjectName("label2")
        label3 = QLabel("page 3")
        label3.setFixedSize(150, 60)
        label3.setObjectName("label3")
        label4 = QLabel("page 4")
        label4.setFixedSize(150, 60)
        label4.setObjectName("label4")

        # use combobox to change stacked widget‘s current index
        self.combobox = QComboBox(self)
        for i in range(4):
            self.combobox.addItem("Page {0}".format(i+1))
        self.combobox.activated.connect(self.stackedWidget.setCurrentIndex)

        self.stackedWidget.addWidget(label1)
        self.stackedWidget.addWidget(label2)
        self.stackedWidget.addWidget(label3)
        self.stackedWidget.addWidget(label4)

if __name__ == __main__:
    app = QApplication(sys.argv)
    form = MystackedWidget()
    form.show()
    app.exec_()

 

以上是关于QStackedWidget实例的主要内容,如果未能解决你的问题,请参考以下文章

QStackedWidget实例

QStackedWidget::setCurrentIndex() 改变场景视口

来自其类外部的 QStackedWidget 的 setCurrentIndex

用QStackedWidget,怎么实现窗口切换

PyQt:如何在 QStackedWidget 中切换小部件

QT如何实现QStackedWidget界面间的切换