My overall goal here is to define have a main app with multiple important widgets, where each widget is defined with a separate python class and loaded from a separate .ui file.
I can't seem to get widget UI to show up when I start the main app when the widget module loads its .ui file directly.
What I am hoping to see is this (Figure 1)
What I seeing is this (Figure 2)
Here is the code that generates Figure 2. It is excerpted from HETP_main.py, which is at the end.
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.topPanelWDG = TopPanel() # set up behaviors for the top panel
self.setupUi(self)
The following code will generate Figure 1 (what I want). However it constructs the main window in python code rather than loading it from a .ui file. This code was generated using pyuic5 but I had to remove the commented-out lines to get it to work.
MainWindow.setObjectName("MainWindow")
MainWindow.resize(649, 130)
MainWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setStyleSheet("QPushButton {background-color: rgb(239, 239, 239)};")
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
# self.topPanelWDG = QtWidgets.QWidget(self.centralwidget) # removed
# self.topPanelWDG.setMinimumSize(QtCore.QSize(0, 75)) # removed
# self.topPanelWDG.setMaximumSize(QtCore.QSize(16777215, 75)) # removed
# self.topPanelWDG.setStyleSheet("background-color: yellow") # removed
# self.topPanelWDG.setObjectName("topPanelWDG") # removed
self.verticalLayout.addWidget(self.topPanelWDG)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# self.retranslateUi(MainWindow) # removed
# QtCore.QMetaObject.connectSlotsByName(MainWindow) # removed
It's pretty clear that I am somehow doubly defining topPanelWDG, but I don't see how or how to fix it. So my question is how do I get Figure 1 while loading from a .ui file.
The full .py and .ui files used here are attached.
The following is the full contents of the .py and the two .ui files.
HETP_main.py
```from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys
Ui_MainWindow, QtBaseClass = uic.loadUiType("main.ui")
class TopPanel(QtWidgets.QWidget):
def __init__(self):
super(TopPanel, self).__init__()
uic.loadUi("toppanel.ui", self)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
construct_method = 'load'
construct_method = 'build2'
if construct_method == 'load':
self.topPanelWDG = TopPanel() # set up behaviors for the top panel
self.setupUi(self)
else:
self.topPanelWDG = TopPanel() # set up behaviors for the top panel
self.construct2(self)
self.setWindowTitle(construct_method)
def construct2(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(649, 130)
MainWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setStyleSheet("QPushButton {background-color: rgb(239, 239, 239)};")
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
# self.topPanelWDG = QtWidgets.QWidget(self.centralwidget) # removed
# self.topPanelWDG.setMinimumSize(QtCore.QSize(0, 75)) # removed
# self.topPanelWDG.setMaximumSize(QtCore.QSize(16777215, 75)) # removed
# self.topPanelWDG.setStyleSheet("background-color: yellow") # removed
# self.topPanelWDG.setObjectName("topPanelWDG") # removed
self.verticalLayout.addWidget(self.topPanelWDG)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# self.retranslateUi(MainWindow) # removed
# QtCore.QMetaObject.connectSlotsByName(MainWindow) # removed
# def retranslateUi(self, MainWindow): # removed
# _translate = QtCore.QCoreApplication.translate # removed
# MainWindow.setWindowTitle(_translate("MainWindow", "HETP Scanning System")) # removed
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_() # should pass command line kwargs?```
toppanel.ui
```<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>654</width>
<height>72</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QFrame" name="vanFrame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>48</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>64</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">
QWidget {background-color: rgb(167, 255, 195)}
.QPushButton {
background-color: LightGray
opacity 0.2;
font: bold;
};</string>
</property>
<property name="frameShape">
<enum>QFrame::Panel</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Top Panel</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>```
main.ui
I had difficulty including this in the question so it is available here. main.ui