In my PyQt5 application I have made a subclass of QMainWindow to customize the main window.
But with this design, I am not able to display the title of the main window and also not able to show the menu bar at the top.
If I uncomment the window.show() statement in my code, that pops-up 2 separate windows - and in one of them it can show and display the title and menu but my purpose is not met.
How can I show the title, menu and the QStackedLayout that I have created in one single window ?
Below is my code :
import sys
from PyQt5.QtCore import QSize, QRect, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QHBoxLayout, QVBoxLayout, QLabel, QWidget, QStackedLayout
from PyQt5.QtGui import QIcon
class Ui(QWidget):
def setupUi(self, Main):
#print (dir(Main))
self.setWindowTitle("My App")
Main.setObjectName("Main")
Main.setFixedSize(900, 500)
self.width = 900
self.height = 500
self.setFixedSize(self.width, self.height)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
self.menu = QStackedLayout()
self.mainMenu = QWidget()
self.nextMenu = QWidget()
self.mainMenuUi()
self.nextMenuUi()
self.menu.addWidget(self.mainMenu)
self.menu.addWidget(self.nextMenu)
def mainMenuUi(self):
print ("Inside mainMenuUi")
self.mainMenu.setFixedSize(self.width, self.height)
self.mainMenuText = QLabel("welcome view", self.mainMenu)
self.mainMenuText.setGeometry(QRect(30, 120, 480, 200))
self.mainMenuText.setStyleSheet("font: 14pt Century Gothic")
self.mainMenuText.setAlignment(Qt.AlignLeading|Qt.AlignLeft|Qt.AlignTop)
self.btn2 = QPushButton("Next", self.mainMenu)
def nextMenuUi(self):
print ("Inside nextMenuUi")
self.nextMenu_layout = QVBoxLayout()
self.nextMenu.setFixedSize(self.width, self.height)
self.btn1 = QPushButton("Back")
self.nextMenu_layout.addWidget(self.btn1)
self.nextMenu.setLayout(self.nextMenu_layout)
# Subclass QMainWindow to customize your application's main window
class MainWindow(QMainWindow, Ui):
def __init__(self):
super().__init__()
self.setWindowTitle("My App window title")
self.setupUi(self)
self.btn1.clicked.connect(self.menuWindow)
self.btn2.clicked.connect(self.menuWindow2)
def menuWindow(self):
self.menu.setCurrentIndex(0)
def menuWindow2(self):
self.menu.setCurrentIndex(1)
app = QApplication(sys.argv)
window = MainWindow()
#window.show()
app.exec()
Qt Main Window Framework
...
Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.
What @musicamante wrote to you looks like this:
import sys
from PyQt5.QtCore import QRect, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, \
QAction, QVBoxLayout, QLabel, QWidget, QStackedLayout, QStyle
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App window title")
self.centralwidget = QWidget()
self.setCentralWidget(self.centralwidget)
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
openFile = QAction(self.style().standardIcon(QStyle.SP_MessageBoxCritical),
'Open', self)
fileMenu.addAction(openFile)
self.width = 900
self.height = 500
self.resize(self.width, self.height)
self.mainMenu = QWidget()
self.nextMenu = QWidget()
self.mainMenuUi()
self.nextMenuUi()
self.menu = QStackedLayout(self.centralwidget)
self.menu.addWidget(self.mainMenu)
self.menu.addWidget(self.nextMenu)
self.btn1.clicked.connect(self.menuWindow)
self.btn2.clicked.connect(self.menuWindow2)
def mainMenuUi(self):
self.mainMenuText = QLabel("welcome view", self.mainMenu)
self.mainMenuText.setGeometry(QRect(30, 120, 480, 200))
self.mainMenuText.setStyleSheet("font: 14pt Century Gothic")
self.mainMenuText.setAlignment(Qt.AlignLeading|Qt.AlignLeft|Qt.AlignTop)
self.btn2 = QPushButton("Next", self.mainMenu)
def nextMenuUi(self):
self.nextMenu_layout = QVBoxLayout()
self.btn1 = QPushButton("Back")
self.nextMenu_layout.addWidget(self.btn1)
self.nextMenu.setLayout(self.nextMenu_layout)
def menuWindow(self):
self.menu.setCurrentIndex(0)
def menuWindow2(self):
self.menu.setCurrentIndex(1)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())