0

I am using a QToolBar for Navigating from page to page. Obviously I am using QMainWindow. The first issue I had was getting QWidgets/layouts on the QMainWindow. I was able to solve this issue with the solution used for this StackOverflow question:

PYQT5 QMainWindow setCentralWidget consisting of multiple box layouts

The solution to the question above does display the layout when the application is first started, but when you click another ToolBar button, and then click the Dashboard ToolBar button it does not return to the layout on the QMainWindow screen.

Start of Application:

enter image description here

TButton One Screen:

enter image description here

In the dashboard() function you will see everything I have tried to get back to the QMainWindow layout. All the other buttons work fine. I just can't seem to figure out how to get back to the layout on the QMainWindow.

The line of code used in the dashboard() function simply displays a blank screen as shown below

Black Dashboard:

enter image description here

How can I return to the layout on the QMainWindow using the Dashboard ToolBar button?

Your help will be greatly appreciated.

Code:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtGui
from PyQt5.QtCore import *
import sys

class ApplicationWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQt5 QMainWindow Example")
        self.setWindowIcon(QtGui.QIcon("IMAGES/python.ico"))
        self.resize(1350, 750)
        self.main_widget = QWidget(self)
        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)
        self.gui()
        
    def gui(self):
        self.main_tool_bar()
        self.widgets()
        self.layouts()
        
    def main_tool_bar(self):
        
        tool_bar = QToolBar()
        tool_bar.setStyleSheet("background-color: #020000; color: #FFFFFF;")
        
        self.addToolBar(Qt.LeftToolBarArea, tool_bar)
        tool_bar.setMovable(False)
        tool_bar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        tool_bar.setIconSize(QSize(40, 40))
        
        dashboard_tool_bar_btn = QAction(QtGui.QIcon("IMAGES/dashboard.png"), "Dashboard", self)
        dashboard_tool_bar_btn.triggered.connect(self.dashboard)
        
        tool_bar_one_btn = QAction(QtGui.QIcon("IMAGES/tool_one.png"), "TButton One", self)
        tool_bar_one_btn.triggered.connect(self.button_one)
        
        tool_bar_two_btn = QAction(QtGui.QIcon("IMAGES/tool_two.png"), "TButton Two", self)
        tool_bar_two_btn.triggered.connect(self.button_two)
        
        tool_bar_three_btn = QAction(QtGui.QIcon("IMAGES/tool_three.png"), "TButton Three", self)
        tool_bar_three_btn.triggered.connect(self.button_three)
        
        tool_bar_four_btn = QAction(QtGui.QIcon("IMAGES/tool_four.png"), "TButton Four", self)
        tool_bar_four_btn.triggered.connect(self.button_four)
        
        tool_bar.addAction(dashboard_tool_bar_btn)
        tool_bar.addAction(tool_bar_one_btn)
        tool_bar.addAction(tool_bar_two_btn)
        tool_bar.addAction(tool_bar_three_btn)
        tool_bar.addAction(tool_bar_four_btn)
        
    def widgets(self):

        # l = QVBoxLayout(self.main_widget)
        
        # l.addWidget(self.ldt_one)
        # l.addWidget(self.ldt_two)      
        
       # Snippet 3
        # x = QHBoxLayout()         # self.main_widget) # new
        
        # x.addWidget(b1)                     # new   + b1
        # x.addWidget(b2)                     # new   + b2
        #
        # l.addLayout(x)
        # l.addStretch()
        
        self.header_lbl = QLabel("QMainWindow Example")
        self.header_lbl.setStyleSheet("color: green")
        
        self.title_lbl = QLabel("Dashboard")
        self.title_lbl.setStyleSheet("font-size: 12pt; font-weight: Normal;")
        
        self.ldt_one = QLineEdit()
        self.ldt_two = QLineEdit()
        
        self.b1 = QPushButton("Button 1") # new
        self.b1.clicked.connect(self.lineedit_one)
        
        self.b2 = QPushButton("Button 2") # new
        self.b2.clicked.connect(self.lineedit_two)
        
    def layouts(self):
        
        self.l = QVBoxLayout(self.main_widget)
        self.header_layout = QHBoxLayout()
        self.title_layout = QHBoxLayout()
        self.x = QHBoxLayout()
        
        self.header_layout.addWidget(self.header_lbl)
        self.header_layout.setContentsMargins(10, 15, 0, 5)
        self.header_layout.addStretch()
        
        self.title_layout.addWidget(self.title_lbl)
        self.title_layout.setContentsMargins(10, 0, 0, 0)
        self.title_layout.addStretch()
        
        self.l.addLayout(self.header_layout)
        self.l.addLayout(self.title_layout)
        
        self.l.addWidget(self.ldt_one)
        self.l.addWidget(self.ldt_two)
         
        self.x.addWidget(self.b1)                     # new   + b1
        self.x.addWidget(self.b2)                     # new   + b2
        
        self.l.addLayout(self.x)
        self.l.addStretch()
        
        self.setLayout(self.l)
        
    def dashboard(self):
        # self.gui()
        # self.show(self.main_widget)
        # self.setCentralWidget(self.main_widget)
        # self.setCentralWidget(self.gui())
        # app_window = ApplicationWindow()
        # app_window.show()
        # app_window = ApplicationWindow(self.main_widget)
        # app_window.show()
        # self.setCentralWidget(app_window)
        # self.main_widget.show()
        # app_ = ApplicationWindow()
        # app_.gui()
        self.setCentralWidget(self.show())
    
    def button_one(self):
        btn_one = ButtonOne()
        self.setCentralWidget(btn_one)
    
    def button_two(self):
        btn_two = ButtonTwo()
        self.setCentralWidget(btn_two)
    
    def button_three(self):
        btn_three = ButtonThree()
        self.setCentralWidget(btn_three)
    
    def button_four(self):
        btn_four = ButtonFour()
        self.setCentralWidget(btn_four)
        
    def lineedit_one(self):
        self.ldt_one.setText("Line Edit One")
        
    def lineedit_two(self):
        self.ldt_two.setText("Line Edit Two")
        
        
class ButtonOne(QWidget):
    def __init__(self):
        super().__init__()
        self.gui()
        
    def gui(self):
        self.widgets()
        self.layouts()
        
    def widgets(self):
        self.header_lbl = QLabel("QMainWindow Example")
        self.header_lbl.setStyleSheet("color: green")
        
        self.title_lbl = QLabel("Tool Button One")
        self.title_lbl.setStyleSheet("font-size: 12pt; font-weight: Normal;")
        
    def layouts(self):
        self.main_layout = QVBoxLayout()
        self.header_layout = QHBoxLayout()
        self.title_layout = QHBoxLayout()
        
        self.header_layout.addWidget(self.header_lbl)
        self.header_layout.setContentsMargins(10, 15, 0, 5)
        self.header_layout.addStretch()
        
        self.title_layout.addWidget(self.title_lbl)
        self.title_layout.setContentsMargins(10, 0, 0, 0)
        self.title_layout.addStretch()
        
        self.main_layout.addLayout(self.header_layout)
        self.main_layout.addLayout(self.title_layout)
        self.main_layout.addStretch()
        
        self.setLayout(self.main_layout)
        
        
class ButtonTwo(QWidget):
    def __init__(self):
        super().__init__()
        self.gui()
        
    def gui(self):
        self.widgets()
        self.layouts()
        
    def widgets(self):
        self.header_lbl = QLabel("QMainWindow Example")
        self.header_lbl.setStyleSheet("color: red")
        
        self.title_lbl = QLabel("Tool Button Two")
        self.title_lbl.setStyleSheet("font-size: 12pt; font-weight: Normal;")
        
    def layouts(self):
        self.main_layout = QVBoxLayout()
        self.header_layout = QHBoxLayout()
        self.title_layout = QHBoxLayout()
        self.table_layout = QVBoxLayout()
        
        self.header_layout.addWidget(self.header_lbl)
        self.header_layout.setContentsMargins(10, 15, 0, 5)
        self.header_layout.addStretch()
        
        self.title_layout.addWidget(self.title_lbl)
        self.title_layout.setContentsMargins(10, 0, 0, 0)
        self.title_layout.addStretch()
        
        self.main_layout.addLayout(self.header_layout)
        self.main_layout.addLayout(self.title_layout)
        self.main_layout.addStretch()
        
        self.setLayout(self.main_layout)
        
        
class ButtonThree(QWidget):
    def __init__(self):
        super().__init__()
        self.gui()
        
    def gui(self):
        self.widgets()
        self.layouts()
        
    def widgets(self):
        self.header_lbl = QLabel("QMainWindow Example")
        self.header_lbl.setStyleSheet("color: #2479E6")
        
        self.title_lbl = QLabel("Tool Button Three")
        self.title_lbl.setStyleSheet("font-size: 12pt; font-weight: Normal;")
        
    def layouts(self):
        self.main_layout = QVBoxLayout()
        self.header_layout = QHBoxLayout()
        self.title_layout = QHBoxLayout()
        self.table_layout = QVBoxLayout()
        
        self.header_layout.addWidget(self.header_lbl)
        self.header_layout.setContentsMargins(10, 15, 0, 5)
        self.header_layout.addStretch()
        
        self.title_layout.addWidget(self.title_lbl)
        self.title_layout.setContentsMargins(10, 0, 0, 0)
        self.title_layout.addStretch()
        
        self.main_layout.addLayout(self.header_layout)
        self.main_layout.addLayout(self.title_layout)
        self.main_layout.addStretch()
        
        self.setLayout(self.main_layout)
        
        
class ButtonFour(QWidget):
    def __init__(self):
        super().__init__()
        self.gui()
        
    def gui(self):
        self.widgets()
        self.layouts()
        
    def widgets(self):
        self.header_lbl = QLabel("QMainWindow Example")
        self.header_lbl.setStyleSheet("color: orange")
        
        self.title_lbl = QLabel("Tool Button Four")
        self.title_lbl.setStyleSheet("font-size: 12pt; font-weight: Normal;")
        
    def layouts(self):
        self.main_layout = QVBoxLayout()
        self.header_layout = QHBoxLayout()
        self.title_layout = QHBoxLayout()
        self.table_layout = QVBoxLayout()
        
        self.header_layout.addWidget(self.header_lbl)
        self.header_layout.setContentsMargins(10, 15, 0, 5)
        self.header_layout.addStretch()
        
        self.title_layout.addWidget(self.title_lbl)
        self.title_layout.setContentsMargins(10, 0, 0, 0)
        self.title_layout.addStretch()
        
        self.main_layout.addLayout(self.header_layout)
        self.main_layout.addLayout(self.title_layout)
        self.main_layout.addStretch()
        
        self.setLayout(self.main_layout)


def main():
    App = QApplication(sys.argv)
    
    App.setStyleSheet(
        
        '''
            
            QToolButton {
                
                margin-top: 5px;
                padding-top: 5px;
            }
            
            QLabel {
                
                font-family: Times;
                font-size: 26pt;
                font-weight: Bold;
                color: #000000;
            }
            
        '''
        
    )
    
    main_window = ApplicationWindow()
    main_window.show()
    sys.exit(App.exec_())

if __name__ == "__main__":
    main()  
    # app = QApplication(sys.argv) 
    # MainWindow = ApplicationWindow() 
    # MainWindow.show() 
    # sys.exit(app.exec_())

grippnault
  • 31
  • 6
  • As explained in the [`setCentralWidget()` documentation](https://doc.qt.io/qt-5/qmainwindow.html#setCentralWidget) "QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time." You shall not use it with an existing central widget unless you want to permanently delete it. Besides, don't overcomplicate things by creating and setting a *new* central widget *every time*, just use a [QStackedWidget](//doc.qt.io/qt-5/qstackedwidget.html). – musicamante May 30 '22 at 08:00
  • Note that the `self.layout(self.l)` at the end of `layouts` is not only unnecessary (the layout was already set on the main widget with its constructor), but wrong, since you cannot set any layout on a QMainWindow. – musicamante May 30 '22 at 08:03

0 Answers0