Load whole *ui file in an frame/widget of another *.ui file
Solution 1:
First of all, the basis of my solution is the promotion of a widget, for this I will structure the project in the following way:
├── main.py
├── pages
│ ├── energypage.py
│ ├── fanpage.py
│ ├── homepage.py
│ └── statuspage.py
└── ui
├── energy.ui
├── fan.ui
├── home.ui
├── main.ui
└── status.ui
the .ui of the pages will be based on the Widget template but the main one will use the MainWindow template (MainWindow allows to have ToolBars, StatusBar, DockWidgets, menuBar, etc so I choose it as main).
Since the .ui can not be promoted by itself will create classes that invoke the design and follow a similar structure but you can add more functionalities, for example in the case of homepage.py:
import os
from PyQt4 import QtGui, uic
current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "../ui/home.ui"))
class HomeWidget(Base, Form):
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
self.setupUi(self)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = HomeWidget()
w.show()
sys.exit(app.exec_())
The main.ui has the buttons on the left side and the QStackedWidget on the right side:
Each page was added by selecting the option insert page-> after current page of the menu that opens by right clicking on the QStackedWidget
Then it will be promoted to use the widget that is in the pages folder:
Then in the main you associate the buttons with the appropriate indexes:
main.py
import os
from PyQt4 import QtGui, uic
from functools import partial
current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))
class MainWidget(Base, Form):
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
self.setupUi(self)
buttons = (self.homebutton, self.statusbutton, self.fanbutton, self.energybutton)
for i, button in enumerate(buttons):
button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
app.setStyle("fusion")
w = MainWidget()
w.show()
sys.exit(app.exec_())
The complete example is here.