简体   繁体   English

如何在 PyQT5 中创建导航栏

[英]How to create navigation bar in in PyQT5

I am about to create a navigation bar in qt designer, but I am not completely sure how to accomplish this.我即将在 qt 设计器中创建一个导航栏,但我不完全确定如何做到这一点。 My first thought is to create 3x Windows, with three buttons on top called General, Delivery, and Payment.我的第一个想法是创建 3 个 Windows,顶部有三个按钮,分别称为 General、Delivery 和 Payment。 Then whenever I click on one of the buttons, I will be directed to the other window.然后,每当我单击其中一个按钮时,我都会被定向到另一个 window。 Is that the ' correct ' way of creating a navigation bar in qt designer?这是在 qt 设计器中创建导航栏的“正确”方式吗?

Another thought I was to create 1x window, but three different frames, so that it would only change a frame when button is clicked, and not the whole window itself.另一个想法是我要创建 1x window,但是三个不同的框架,这样它只会在单击按钮时改变一个框架,而不是整个 window 本身。 Unfortunately, I am completely lost in how to do so.不幸的是,我完全不知道该怎么做。

An example of the layout:布局示例:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

One possible solution is to use a modified QTabWidget and create custom pages:一种可能的解决方案是使用修改后的 QTabWidget 并创建自定义页面:

from functools import cached_property
import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class Page(QtWidgets.QWidget):
    completeChanged = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.container)
        lay.addWidget(self.button, alignment=QtCore.Qt.AlignCenter)

        self.button.clicked.connect(self.handle_clicked)

    @cached_property
    def container(self):
        return QtWidgets.QWidget()

    @cached_property
    def button(self):
        return QtWidgets.QPushButton("Save")

    def handle_clicked(self):
        if self.validate():
            self.completeChanged.emit()

    def validate(self):
        # Override this method if you want to validate the entries, 
        # if it returns True then it will go to the next page, 
        # otherwise it will not move from the page
        return True


class TabWizard(QtWidgets.QTabWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.tabBar().installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj is self.tabBar() and event.type() == QtCore.QEvent.MouseButtonPress:
            return True
        return super().eventFilter(obj, event)

    def addPage(self, page, title):
        if not isinstance(page, Page):
            raise TypeError(f"{page} must be Page object")
        self.addTab(page, title)
        page.completeChanged.connect(self.nextPage)

    def nextPage(self):
        next_index = self.currentIndex() + 1
        if next_index < self.count():
            self.setCurrentIndex(next_index)


class Page1(Page):
    def __init__(self, parent=None):
        super().__init__(parent)

        lay = QtWidgets.QFormLayout(self.container)
        lay.addRow("Foo1", QtWidgets.QLineEdit())
        lay.addRow("Bar1", QtWidgets.QLineEdit())


class Page2(Page):
    def __init__(self, parent=None):
        super().__init__(parent)

        lay = QtWidgets.QFormLayout(self.container)
        lay.addRow("Foo2", QtWidgets.QLineEdit())
        lay.addRow("Bar2", QtWidgets.QLineEdit())


class Widget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        tabwizard = TabWizard()
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(tabwizard)

        tabwizard.addPage(Page1(), "page1")
        tabwizard.addPage(Page2(), "page2")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM