Doss Gunter
Doss Gunter

Reputation: 89

PyQt5: Progamically adding QWidget to layout, doesn't show Qwidget when a spacer is added

Edit: 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.

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.

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.

I have looked on google quite a bit and tried a lot of suggestions.

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

I tried setting the minimum sizes and sizing polices to various things.

Below is my current code, any ideas or suggestions would be appreciated!

#!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_())

here is a link to the ui files (they are to long to post): gist link for ui files

Upvotes: 0

Views: 496

Answers (1)

Doss Gunter
Doss Gunter

Reputation: 89

I finally fixed this by trying random things over and over until something worked.

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.)

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.

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!

enter image description here

Upvotes: 1

Related Questions