我尝试将多个小部件添加到滚动区域中,其中包含用于拆分文本和按钮的垂直布局以及用于拆分按钮的水平布局。
在我的期望中,所有的小部件应该是一个一个的,如果小部件太多,滚动区域可以滚动。
我使用容器垂直布局来包含我尝试放入滚动区域的所有小部件,然后将容器布局放入容器小部件中。之后,我用容器小部件在滚动区域设置了一个小部件,但结果不是我所期望的;相反,布局中的所有小部件都折叠在一起,我几乎看不到文字,也无法滚动该区域。
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QScrollArea, QSizePolicy, QLayout
from UI import comment # Assuming you have saved the form in a file named comment_form.py
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("Scroll Area Example")
window.setGeometry(100, 100, 400, 400)
# Create a container widget and layout to hold the forms
container_widget = QWidget()
container_layout = QVBoxLayout()
container_layout.setSpacing(10) # Adjust the spacing as needed
for i in range(5):
# Create instances of the Ui_Form
form = comment.Ui_Form()
form_widget = QWidget()
form.setupUi(form_widget)
form.Comment_label.setWordWrap(True) # Allow text to wrap to multiple lines
form.Comment_label.setFixedWidth(300)
form.Comment_label.setText("Test message in scroll area" * i)
form_widget.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Minimum) # Set the size policy
container_layout.addWidget(form_widget)
container_layout.setSizeConstraint(QLayout.SizeConstraint.SetMaximumSize)
container_widget.setLayout(container_layout)
# Create a scroll area and set the container widget as its content
scroll_area = QScrollArea()
scroll_area.setMinimumSize(350, 300)
scroll_area.setWidgetResizable(True) # Allows the scroll area to resize its contents
scroll_area.setWidget(container_widget)
window_layout = QVBoxLayout()
window_layout.addWidget(scroll_area)
window.setLayout(window_layout)
window.show()
sys.exit(app.exec())
# Form implementation generated from reading ui file 'comment.ui'
#
# Created by: PyQt6 UI code generator 6.5.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.verticalLayoutWidget_2 = QtWidgets.QWidget(parent=Form)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(10, 10, 199, 80))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setSizeConstraint(QtWidgets.QLayout.SizeConstraint.SetFixedSize)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SizeConstraint.SetFixedSize)
self.verticalLayout.setObjectName("verticalLayout")
self.Author_label = QtWidgets.QLabel(parent=self.verticalLayoutWidget_2)
self.Author_label.setWordWrap(True)
self.Author_label.setObjectName("Author_label")
self.verticalLayout.addWidget(self.Author_label)
self.Comment_label = QtWidgets.QLabel(parent=self.verticalLayoutWidget_2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Preferred, QtWidgets.QSizePolicy.Policy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.Comment_label.sizePolicy().hasHeightForWidth())
self.Comment_label.setSizePolicy(sizePolicy)
self.Comment_label.setScaledContents(True)
self.Comment_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeading|QtCore.Qt.AlignmentFlag.AlignLeft|QtCore.Qt.AlignmentFlag.AlignTop)
self.Comment_label.setWordWrap(True)
self.Comment_label.setObjectName("Comment_label")
self.verticalLayout.addWidget(self.Comment_label)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SizeConstraint.SetFixedSize)
self.horizontalLayout.setObjectName("horizontalLayout")
self.Reply_pushButton = QtWidgets.QPushButton(parent=self.verticalLayoutWidget_2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.Reply_pushButton.sizePolicy().hasHeightForWidth())
self.Reply_pushButton.setSizePolicy(sizePolicy)
self.Reply_pushButton.setObjectName("Reply_pushButton")
self.horizontalLayout.addWidget(self.Reply_pushButton)
self.Delete_pushButton = QtWidgets.QPushButton(parent=self.verticalLayoutWidget_2)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.Delete_pushButton.sizePolicy().hasHeightForWidth())
self.Delete_pushButton.setSizePolicy(sizePolicy)
self.Delete_pushButton.setObjectName("Delete_pushButton")
self.horizontalLayout.addWidget(self.Delete_pushButton)
self.verticalLayout.addLayout(self.horizontalLayout)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.Author_label.setText(_translate("Form", "TextLabel"))
self.Comment_label.setText(_translate("Form", "TextLabel"))
self.Reply_pushButton.setText(_translate("Form", "Reply"))
self.Delete_pushButton.setText(_translate("Form", "Delete"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec())
经过我自己的探索,我发现这篇文章给了我启发。 然后,我将这一行添加到代码中,它就可以工作了!
form_widget.setMinimumHeight(65+form.Comment_label.sizeHint().height())
此代码表示第一个标签高度+按钮高度+评论标签高度,并且可以自动获取评论标签的高度以适应小部件,因为
wordWrap
为True。