I am currently working on a project that involves developing a GUI, that I can send emails to someone and if I need I should be able to save it on a database. The GUI has two pages, the senders credentials are entered in first page and the email is sent using second page. I made UI for first and second pages using Qt designer. And I made a UI (MainWindow) containing a stacked widget, so that I can stack the page1 and page2 in it.
I choose to do it this way because I want to switch from pages anytime I want and the attributes related to page1 and page2 are supposed to be used at the same time.(I need to access them using one class, that is MainApp class). But when I try to stack page1 and page2 to the stackedWidget it doesn't give an error, but it doesn't load the pages also. Can someone point out what I am doing wrong here?
from PyQt5 import uic
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
import os
class MainApp(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
uic.loadUi('designs/win_stacked.ui', self)
self.win_1 = win1()
self.win_2 = win2()
self.stackedWidget.addWidget(self.win_1)
print('why stacked widget is not loaded with other windows??')
self.stackedWidget.addWidget(self.win_2)
class win1(QtWidgets.QMainWindow):
def __init__(self):
super(win1, self).__init__()
uic.loadUi('designs/win1.ui', self)
class win2(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
uic.loadUi('designs/win2.ui', self)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
wind = MainApp()
wind.show()
sys.exit(app.exec_())
(Link to all the UI files: https://drive.google.com/drive/folders/1UC8TVCsmwaQfksd6MtPf-9tGXxtOpH45?usp=sharing).
The problem is largely caused by using a
QMainWindowfor the pages in the stacked-widget. These pages should just be a plainQWidgetinstead. There are also a few issues with the layouts. I have fixed all these issues in the solution shown below, and also added a button-handler to show how to switch pages. Hopefully this should help get you started. To test things properly, please make sure you use all the files provided below:win_stacked.ui:
win1.ui:
win2.ui: