PyQt5中动态添加的Widgets

Dynamically added Widgets in PyQt5

我是 PyQt 的新手,我正在尝试创建一个系统,当用户按下搜索按钮时,它会动态添加小部件。添加的项目应该用于显示搜索结果。但是,添加的项目被包裹在某个矩形中,而不是增加布局的大小。

我的代码如下:

Ui_test.py(由 Qt Designer 创建)

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Learnnote(object):
    def setupUi(self, Learnnote):
        Learnnote.setObjectName("Learnnote")
        Learnnote.setEnabled(True)
        Learnnote.resize(524, 345)
        self.centralwidget = QtWidgets.QWidget(Learnnote)
        self.centralwidget.setObjectName("centralwidget")
        self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
        self.scrollArea.setGeometry(QtCore.QRect(10, 50, 491, 241))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 489, 239))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.pushButton_Search = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_Search.setGeometry(QtCore.QRect(10, 0, 141, 41))
        self.pushButton_Search.setObjectName("pushButton_Search")
        Learnnote.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(Learnnote)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 524, 22))
        self.menubar.setObjectName("menubar")
        Learnnote.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(Learnnote)
        self.statusbar.setObjectName("statusbar")
        Learnnote.setStatusBar(self.statusbar)

        self.retranslateUi(Learnnote)
        self.pushButton_Search.clicked.connect(self.pushButton_Search_click)
        QtCore.QMetaObject.connectSlotsByName(Learnnote)

    def retranslateUi(self, Learnnote):
        _translate = QtCore.QCoreApplication.translate
        Learnnote.setWindowTitle(_translate("Learnnote", "MainWindow"))
        self.pushButton_Search.setText(_translate("Learnnote", "Search"))

main.py:

from PyQt5.QtWidgets import QApplication,QMainWindow
from Ui_test import Ui_Learnnote
from PyQt5 import QtWidgets
import sys

class ExampleWidget(QtWidgets.QGroupBox):
    def __init__(self, numAddWidget):
        QtWidgets.QGroupBox.__init__(self)
        self.numAddWidget = numAddWidget
        self.numAddItem   = 1
        self.setTitle("Title {}".format(self.numAddWidget)) 
        self.initSubject()
        self.organize()
    def initSubject(self):
        self.lblName = QtWidgets.QLabel("Label Title {}".format(self.numAddWidget), self)
    def organize(self):
        grid = QtWidgets.QGridLayout(self)
        self.setLayout(grid)
        grid.addWidget(self.lblName, 0, 0, 1, 3)


class MyWindow(QMainWindow,Ui_Learnnote):
    def __init__(self, parent=None):
        super(MyWindow,self).__init__(parent)
        self.setupUi(self)
        self.scrollArea.setWidgetResizable(True)
        self.numAddWidget = 1
        self.widget = ExampleWidget(self.numAddWidget)
        self.main_layout = QtWidgets.QVBoxLayout()
        self.main_layout.addWidget(self.widget)
        main_widget = QtWidgets.QWidget(self.scrollArea)
        main_widget.setLayout(self.main_layout)


    def pushButton_Search_click(self):
        self.numAddWidget += 1
        self.widget = ExampleWidget(self.numAddWidget)
        self.main_layout.addWidget(self.widget)
if __name__=="__main__":
    app=QApplication(sys.argv)
    myWin = MyWindow()
    myWin.show()
    sys.exit(app.exec_())

主要问题是您正在创建一个小部件作为滚动区域的直接子项,而实际的可滚动小部件已经存在,因为它是由 Designer 创建的:self.scrollAreaWidgetContents

因此,只需为此创建主要布局,无需进一步的容器小部件(删除 main_widget):

self.main_layout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)

请注意,您还需要为main window(或者更准确地说,它的central widget)设置一个main layout,否则其他内容调整 window 大小时不会正确调整大小。右键单击主要 window 的任何小部件之外的空白区域,然后 select 从“布局”菜单中选择适当的布局。