pythonqtpyqtqvboxlayout

(PyQt) QVBoxLayout shrink upon multiple loading of addWidget


Why is the layout shrinking like this and other times going back to normal?

enter image description here

I've created several separate UI files in QtDesigner, one is the MainWindow and the other is a widget for Loading Data.

In order to work with these files, I've created separate child classes of each UI file. In order to add a new widget to the MainWindow I've created a addWidget() function; it works by adding a particular widget to the scrollarea layout. You can see this function in MainWindow.py


Here is the code for __main__.py

import multiprocessing as mp
import os.path
import sys
import time

from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *

from point_spectra_gui.future_.functions import *
from point_spectra_gui.future_.util import delete
from point_spectra_gui.future_.util.excepthook import my_exception_hook


def new():
    p = mp.Process(target=main, args=())
    p.start()


def connectWidgets(ui):
    ui.actionLoad_Data.triggered.connect(lambda: ui.addWidget(LoadData.Ui_Form))


def main():
    sys._excepthook = sys.excepthook
    sys.excepthook = my_exception_hook

    app = QtWidgets.QApplication(sys.argv)
    mainWindow = QtWidgets.QMainWindow()
    ui = MainWindow.Ui_MainWindow()
    ui.setupUi(mainWindow)
    connectWidgets(ui)
    mainWindow.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()


Here is the code for MainWindow.py

from PyQt5 import QtWidgets

from point_spectra_gui.future_.functions import *
from point_spectra_gui.future_.util import *
from point_spectra_gui.ui import MainWindow


class Ui_MainWindow(MainWindow.Ui_MainWindow):
    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow
        super().setupUi(MainWindow)  # Run the basic window UI
        self.menu_item_shortcuts()  # set up the shortcuts

    def addWidget(self, object):
        widget = object()
        widget.setupUi(self.scrollArea)
        self.widgetLayout = QtWidgets.QVBoxLayout()
        self.widgetLayout.setObjectName("widgetLayout")
        self.verticalLayout_3.addLayout(self.widgetLayout)
        self.widgetLayout.addWidget(widget.get_widget())

    def menu_item_shortcuts(self):
        self.actionExit.setShortcut("ctrl+Q")
        self.actionCreate_New_Workflow.setShortcut("ctrl+N")
        self.actionOpen_Workflow.setShortcut("ctrl+O")
        self.actionRestore_Workflow.setShortcut("ctrl+R")
        self.actionSave_Current_Workflow.setShortcut("ctrl+S")


Here is the code of the child class LoadData.py

from PyQt5 import QtWidgets

from point_spectra_gui.ui.LoadData import Ui_loadData


class Ui_Form(Ui_loadData):
    def setupUi(self, Form):
        super().setupUi(Form)
        self.connectWidgets()

    def get_widget(self):
        return self.groupBox

    def connectWidgets(self):
        self.newFilePushButton.clicked.connect(lambda: self.on_getDataButton_clicked())
        # self.get_data_line_edit.textChanged.connect(lambda: self.get_data_params())
        # self.dataname.textChanged.connect(lambda: self.get_data_params())

    def on_getDataButton_clicked(self):
        filename, _filter = QtWidgets.QFileDialog.getOpenFileName(None, "Open Data File", '.', "(*.csv)")
        self.fileNameLineEdit.setText(filename)
        if self.fileNameLineEdit.text() == "":
            self.fileNameLineEdit.setText("*.csv")


**Edit

Upon trying this again and then shrinking the window. The layout goes back to normal. This to me tells me it's not a problem with my code, it's the way the Qt handles the adding of widgets. I still do not understand why this is happening though. So any insight into how this is happening is very much appreciated.

enter image description here


Solution

  • This problem is with the Form.resize() inside the generated code.

    class Ui_Form(object):
        def setupUi(self, Form):
            Form.setObjectName("Form")
            Form.resize(400, 300)
    

    To fix this you'll need to go into QtDesigner and set the geometry back to it's default the Layout size by clicking the red circled item.

    This, in essence, deletes the resize method call

    click on where the red circle is

    You can then convert again with pyuic