为什么布局会像这样缩小,有时会恢复正常?
我在QtDesigner中创建了几个单独的UI文件,一个是MainWindow,另一个是加载数据的小部件。
为了使用这些文件,我为每个UI文件创建了单独的子类。为了向MainWindow添加一个新的小部件,我创建了一个addWidget()
函数;它的工作原理是向scrollarea布局添加一个特定的小部件。您可以在MainWindow.py
<小时/>
__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()
<小时/>
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")
<小时/>
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")
<小时/>
再次尝试此操作然后收缩窗口。布局恢复正常。 这对我来说告诉我这不是我的代码的问题,它是Qt处理小部件添加的方式。我仍然不明白为什么会发生这种情况。因此,非常感谢任何有关如何发生这种情况的见解。