我可以运行这段代码:
import sys
from PyQt5.QtWidgets import QApplication, QScrollArea, QWidget, QGridLayout, QVBoxLayout, QLabel
from PyQt5 import QtGui, QtCore
from PyQt5.QtGui import QDrag
class MainWidget(QWidget):
def __init__(self, *args, **kwargs):
super().__init__()
self.setStyleSheet("border: 1px solid black;")
self.layout = QGridLayout(self)
self.resize(600, 400)
# for i in [(0,0) , (0,1) , (1,0) , (1,1)]:
for i in [(0,0)]:
print(str(i))
wig = QWidget()
wig.resize(150,100)
# VBox = QVBoxLayout()
# VBox.addStretch(1)
# VBox.addWidget(label)
# wig.setLayout(VBox)
scroll = QScrollArea(wig)
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scrollAreaWidgetContents = QWidget()
# label = QLabel()
# font = self.font()
# font.setPointSize(15)
# label.setFont(font)
# label.setText(str(i)+' ')
# VBox = QVBoxLayout()
# VBox.addWidget(label)
# scrollAreaWidgetContents.setLayout(VBox)
scroll.setWidget(scrollAreaWidgetContents)
self.layout.addWidget(scroll, i[0] , i[1])
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWidget()
sys.exit(app.exec_())
并且能够调整我的小部件以及 QGridlayout 中包含的小部件的大小。
使用PyQt-Designer编写我的应用程序(
test003.ui
)时,通过pyuic5
转换后我得到:
test003.py
:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(658, 538)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
Form.setSizePolicy(sizePolicy)
Form.setSizeIncrement(QtCore.QSize(1, 1))
Form.setStyleSheet("QWidget{background-color: yellow\n"
"}")
self.gridLayoutWidget = QtWidgets.QWidget(Form)
self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 10, 641, 501))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea = QtWidgets.QScrollArea(self.gridLayoutWidget)
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents_2 = QtWidgets.QWidget()
self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 614, 495))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents_2.sizePolicy().hasHeightForWidth())
self.scrollAreaWidgetContents_2.setSizePolicy(sizePolicy)
self.scrollAreaWidgetContents_2.setStyleSheet("QWidget{background-color: blue}")
self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
self.scrollArea.setWidget(self.scrollAreaWidgetContents_2)
self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
以及主要代码:
import sys
from PyQt5.QtWidgets import QApplication, QScrollArea, QWidget, QGridLayout, QVBoxLayout
from PyQt5 import QtGui
from PyQt5.QtGui import QDrag
# from untitled001 import Ui_Form
# from untitled002 import Ui_Form
# from untitled003b import Ui_Form
# from test001 import Ui_Form # form
# from test002 import Ui_Form #form + grid
from test003 import Ui_Form #form + grid + widget
class MainWidget(QWidget, Ui_Form):
def __init__(self, *args, **kwargs):
super().__init__()
self.setupUi(self)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWidget()
# w.show()
sys.exit(app.exec_())
但我得到:
蓝色小部件不与主部件一起调整大小,
我做错了什么? 无法在 PyQt 设计器中找到任何提示并读出
test003.py
ui
在这里找到答案:pyqt5:无法在窗口调整大小时最大化小部件
引用其中一条评论:
您仅为正在创建的“布局小部件”设置布局,但这些小部件不受父布局管理,而父布局应在中央小部件上设置。右键单击任何其他小部件外部的空白区域,然后从“布局”子菜单中选择适当的布局。 –
这里是我的应用程序的设计器工作解决方案:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(704, 500)
Form.setStyleSheet("")
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea_1 = QtWidgets.QScrollArea(Form)
self.scrollArea_1.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea_1.setWidgetResizable(True)
self.scrollArea_1.setObjectName("scrollArea_1")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 667, 484))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents.sizePolicy().hasHeightForWidth())
self.scrollAreaWidgetContents.setSizePolicy(sizePolicy)
self.scrollAreaWidgetContents.setStyleSheet("")
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.widget = QtWidgets.QWidget(self.scrollAreaWidgetContents)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
self.widget.setSizePolicy(sizePolicy)
self.widget.setStyleSheet("QWidget{border: 3px solid red}\n"
"\n"
"QWidget{background-color: green}")
self.widget.setObjectName("widget")
self.horizontalLayout_2.addWidget(self.widget)
self.scrollArea_1.setWidget(self.scrollAreaWidgetContents)
self.gridLayout.addWidget(self.scrollArea_1, 1, 1, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
图片: