布局之间的PyQT导航

时间:2018-07-15 09:14:04

标签: python pyqt5

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

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)

2 个答案:

答案 0 :(得分:1)

第一个DashboardWindow和RaportWindow不是小部件,它们是填充您可能使用Qt Designer创建的小部件的类,您必须做的是创建一个从小部件继承的类并使用以前的类来填充它。

至此,您必须使用QStackedWidget才能交换小部件。

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

答案 1 :(得分:0)

我认为此解决方案可能有用

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(0, 30, 500, 500)
        self.init_ui()
        self.show()

    def init_ui(self):
        self.central_widget = QStackedWidget()

        self.setCentralWidget(self.central_widget)

        self.report_screen = ReportWindow(self)
        self.company_screen = CompanyWindow(self)

        self.central_widget.addWidget(self.report_screen)
        self.central_widget.addWidget(self.company_screen)

        self.central_widget.setCurrentWidget(self.report_screen)

        self.report_screen.clicked.connect(lambda: 
        self.central_widget.setCurrentWidget(self.company_screen))
        self.company_screen.clicked.connect(lambda: 
        self.central_widget.setCurrentWidget(self.report_screen))


class Report(object):
    clicked = pyqtSignal()

    def detail_report(self):
        self.layout = QHBoxLayout()
        self.button = QPushButton('test1')
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)
        self.button.clicked.connect(self.clicked.emit)


class ReportWindow(QWidget, Report):
    def __init__(self, parent=None):
        super(ReportWindow, self).__init__(parent)
        self.detail_report()


class Company(object):
    clicked = pyqtSignal()

    def list_companies(self):
        self.layout = QHBoxLayout()
        self.button = QPushButton('test2')
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)
        self.button.clicked.connect(self.clicked.emit)


class CompanyWindow(QWidget, Company):
    clicked = pyqtSignal()
    def __init__(self, parent=None):
        super(CompanyWindow, self).__init__(parent)
        self.list_companies()


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = App()
    w.show()
    sys.exit(app.exec_())