PyQt5: Progamically adding QWidget to layout, doesn't show Qwidget when a spacer is added
<p><strong>Edit</strong>: I have tried a few more things.  If I move the spacer to a layout below the spacer I am adding to it doesn't exibit the same exact behavior. Its still not optimal, and isn't going to work because my end goal is to have a scrollArea with a spacer inside that i add my widgets to, but this would not look right.  I think the problem is the widgets are getting to a size zero I just do not know why or how to fix it.</p>

<p>I have two ui files made in QtDesigner.  The first file is my main window at the start of the program I load the second ui file and place it into a vertical spacer in the middle of the first one. Additional copies are placed each time a button is clicked.</p>

<p>This all works great until I add a vertical spacer to push the items to the top.  I have tired adding it from Designer and in the code.  Both have the same result.</p>

<p>I have looked on google quite a bit and tried a lot of suggestions.</p>

<p>I tried setting the second ui files parent as a Qwidget I added on the first that contained the vertical layout.</p>

<p>I tried setting the minimum sizes and sizing polices to various things.</p>

<p>Below is my current code, any ideas or suggestions would be appreciated!</p>

<pre><code>#!python3
import sys
from PyQt5 import QtWidgets, QtCore, uic

class TimesheetWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(TimesheetWidget, self).__init__(parent)
        self.parent = parent
        self.tableRows = dict()

    def setup(self):
        self.labelSaved.hide()
        self.addTableRow()

    def addTableRow(self):
        thisRow = len(self.tableRows)
        self.tableRows[thisRow] = uic.loadUi("gui/tableRow.ui")
        self.tableRows[thisRow].addButton.clicked.connect(self.addTableRow)
        self.spacer.addWidget(self.tableRows[thisRow])

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    timesheet = TimesheetWidget()
    Mytimesheet = uic.loadUi("gui/timesheet.ui", baseinstance=timesheet)
    Mytimesheet.setup()
    Mytimesheet.show()

    sys.exit(app.exec_())
</code></pre>

<p>here is a link to the ui files (they are to long to post):
<a href="https://gist.github.com/dossjh/95d14b15c63cee6bb0b5d2db631b755d" rel="nofollow">gist link for ui files</a></p>

<p>I finally fixed this by trying random things over and over until something worked.</p>

<p>It turned out that on my second ui file I did not have a top level layout. (I am not sure if that's what its called.)</p>

<p>To fix this I right clicked on the top level widget and choose layout and selected horizontal layout, although I think any would have worked.</p>

<p>Here is a picture that shows the top level widget with the cancel symbol on it.  Once I added the layout that went away and everything worked!</p>

<p><a href="http://i.stack.imgur.com/0UOYk.png" rel="nofollow"><img src="http://i.stack.imgur.com/0UOYk.png" alt="enter image description here"></a></p>

python pyqt5