布局之间的 PyQT 导航 [英] PyQT Navigation between layouts

查看:47
本文介绍了布局之间的 PyQT 导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的应用程序代码,它允许您在窗口之间切换.该菜单有两个编程选项,例如详细报告"和所有公司",现在加载布局后我不知道如何将按钮放在这两个视图中,以便您将视图从详细报告"更改为全部"公司,反之亦然.你能帮助我吗 :) ?

Below is my application code which allows you to switch between windows. The menu has two options programmed such as 'Detail Report' and 'All companies' and now after loading the layout I do not know how I can put the button in these 2 views that would allow you to change the view from Detail Report to All Companies and vice versa. Can you help me :) ?

class App(QMainWindow):
def __init__(self):
    super().__init__()
    self.dashboardView = DashboardWindow()
    self.detailView = RaportWindow()
    self.infoView = InfoWindow()
    self.init_ui()
    self.show()

def init_ui(self):

    main_menu = self.menuBar()
    dashboard = main_menu.addMenu('Dashboard')

    dashboard.addAction(QAction('Detail Raport', self))
    dashboard.addAction(QAction('All companies', self))
    dashboard.triggered.connect(self.change_view)
    self.setWindowTitle(self.title)
    self.show()


def change_view(self, q):
    if q.text() == 'Detail Raport':
        self.detailView.detailRaport(self)
        self.show()
    if q.text() == 'All companies':
        self.dashboardView.setupUIdashboard(self)
        self.show()


class RaportWindow(object):
def detailRaport(self, MainWindow):
    self.centralwidget = QWidget(MainWindow)
    grid = QGridLayout()
    ...
    self.centralwidget.setLayout(grid)
    MainWindow.setCentralWidget(self.centralwidget)

class DashboardWindow(object):
def setupUIdashboard(self, MainWindow):
    self.centralwidget = QWidget(MainWindow)
    grid = QGridLayout()
    .....

    self.centralwidget.setLayout(grid)
    scrollArea = QScrollArea()
    scrollArea.setWidgetResizable(True)
    scrollArea.setWidget(self.centralwidget)
    MainWindow.setCentralWidget(scrollArea)

推荐答案

首先 DashboardWindow 和 RaportWindow 不是小部件,它们是填充小部件的类,您可能使用 Qt Designer 创建的小部件,您必须做的是创建一个继承的类来自一个小部件并使用之前的类来填充它.

First DashboardWindow and RaportWindow are not widgets, they are classes that fill widgets that you have probably created using Qt Designer, what you must do is create a class that inherits from a widget and uses the previous classes to fill it.

切入正题,必须使用QStackedWidget才能交换widget.

Going to the point, you must use QStackedWidget to be able to exchange widgets.

class App(QMainWindow):
    def __init__(self):
        super().__init__()

        self.init_ui()
        self.show()

    def init_ui(self):
        main_menu = self.menuBar()
        dashboard = main_menu.addMenu('Dashboard')
        dashboard.addAction(QAction('Detail Raport', self))
        dashboard.addAction(QAction('All companies', self))
        dashboard.triggered.connect(self.change_view)

        self.dashboardView = Dashboard()
        self.detailView = Raport()

        self.stacked = QStackedWidget()
        self.setCentralWidget(self.stacked)
        self.stacked.addWidget(self.detailView)
        self.stacked.addWidget(self.dashboardView)

    def change_view(self, q):
        if q.text() == 'Detail Raport':
            self.stacked.setCurrentWidget(self.detailView)
        elif q.text() == 'All companies':
            self.stacked.setCurrentWidget(self.dashboardView)

class RaportWindow(object):
    def detailRaport(self, MainWindow):
        ...

class DashboardWindow(object):
    def setupUIdashboard(self, MainWindow):
        ...

class Dashboard(QMainWindow, DashboardWindow):
    def __init__(self, parent=None):
        super(Dashboard, self).__init__(parent)
        self.setupUIdashboard(self)

class Raport(QMainWindow, RaportWindow):
    def __init__(self, parent=None):
        super(Raport, self).__init__(parent)
        self.detailRaport(self)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = App()
    w.show()
    sys.exit(app.exec_())

这篇关于布局之间的 PyQT 导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆