python-3.xqtpyqt5qstackedwidget

How can I show different pages in one window by PyQt5?


Window Snapshot

As you can see, I create a window with left buttons, but I don't know how to use QStackedWidget to show different views after pressed every button. I searched several similar questions, but they were not useful for me. I am new to PyQt5. I add my code below, please have a look.

Here is my code:

class StackedWidget(QStackedWidget):
    def __init__(self, parent = None):
        QStackedWidget.__init__(self, parent)

    def setCurrentIndex(self, index):
        QStackedWidget.setCurrentIndex(self, index)

    def setSend(self):
        self.setCurrentIndex(0)

    def setHome(self):
        self.setCurrentIndex(1)

class Homewindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.style = """
                QPushButton{
                    background-color:rgba(0,0,0,20);
                }
                QPushButton:hover {
                    background-color: rgba(0,0,0,40);
                    color: white;
                }
            """
        self.setStyleSheet(self.style)
        self.initUI()

    def initUI(self):
        self.setGeometry(300,100,804,634)
        self.setWindowTitle('Home')

        self.stack = StackedWidget()

        page1 = QLabel("Page1")
        self.stack.addWidget(page1)

        email = QLabel("Page2")
        self.stack.addWidget(email)

        titleBarLbl = QLabel(self)
        titleBarLbl.setStyleSheet("background-color:blue")
        titleBarLbl.resize(805,53)

        self.sendBtn = QPushButton(self)
        self.sendBtn.setStyleSheet("background-color:red")
        self.sendBtn.clicked.connect(self.stack.setSend)
        self.sendBtn.setGeometry(0,53,48,48)

        self.homeBtn = QPushButton(self)
        self.homeBtn.setStyleSheet("background-color:green")
        self.homeBtn.clicked.connect(self.stack.setHome)
        self.homeBtn.setGeometry(0,101,48,48)

        self.show()

    def mousePressEvent(self,event):
        if event.button() == Qt.LeftButton:
            self.moving = True;
            self.offset = event.pos()

    def mouseMoveEvent(self,event):
        if self.moving:
            self.move(event.globalPos()-self.offset)

Solution

  • The code in your question seems to work okay, except that you did not give the stack-widget a parent, or set its geometry. So just do something like this:

    def initUI(self):
        ...
        self.stack = StackedWidget(self)
        self.stack.setGeometry(55, 0, 750, 600)
    

    A generic way to change the pages in a stack-widget using buttons is to use a QButtonGroup. Each button can be linked to the index of a given page in the stack-widget. Then the buttonClicked signal of the button-group can be used to change the page:

    self.group = QtWidgets.QButtonGroup(self)
    self.group.addButton(self.buttonA, 0)
    self.group.addButton(self.buttonB, 1)
    self.group.addButton(self.buttonC, 2)
    # etc
    self.group.buttonClicked[int].connect(self.stack.setCurrentIndex)
    

    If necessary, the index of the page in the stack-widget can be obtained like this:

    index = self.stack.indexOf(self.pageA)