我一直在尝试将一个/多个QGroupBox插入一个Qscrolla区域
问题是:滚动条不显示。
这是我的密码:
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtWidgets import QGroupBox
from PyQt5.QtWidgets import QScrollArea
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QGridLayout
import sys
lst = [u"D", u"E", u"EF", u"F", u"FG", u"G", u"H", u"JS", u"J", u"K", u"M", u"P", u"R", u"S", u"T", u"U", u"V", u"X", u"Y", u"Z"]
class MyApp(QWidget):
def __init__(self):
super(MyApp, self).__init__()
window_width = 1200
window_height = 600
self.setFixedSize(window_width, window_height)
self.initUI()
def createLayout_group(self):
self.groupbox = QGroupBox(u"Group1:")
self.layout_groupbox = QVBoxLayout()
for i in range(len(lst)):
self.item = QCheckBox(lst[i], self.groupbox)
self.layout_groupbox.addWidget(self.item)
self.layout_groupbox.addStretch(1)
self.groupbox.setLayout(self.layout_groupbox)
def createLayout_Container(self):
self.scrollarea = QScrollArea(self)
self.scrollarea.setFixedSize(250, 6000)
self.scrollarea.setWidgetResizable(False)
self.layout_SArea = QVBoxLayout()
self.layout_SArea.addWidget(self.groupbox)
self.layout_SArea.addWidget(self.groupbox) # add groupbox one more to test
self.layout_SArea.addWidget(self.groupbox) # add groupbox one more to test
self.layout_SArea.addStretch(1)
self.scrollarea.setLayout(self.layout_SArea)
def initUI(self):
self.createLayout_group() # load one groupbox
self.createLayout_Container() # load groupbox container
self.layout_All = QVBoxLayout()
self.layout_All.addWidget(self.scrollarea)
self.setLayout(self.layout_All)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyApp()
sys.exit(app.exec_())
我想达到这个效果。(仅用于表示)但上面的代码没有显示滚动条
您的代码存在以下问题:
您不必向QScrollArea添加布局,您必须传递的是一个小部件,因此您构建的布局必须添加到小部件,并且该小部件必须设置为QScrollArea
每次调用createLayout_组时,都会覆盖类self.groupbox、self.layout_groupbox等的成员,这是不必要的,并且可能会导致问题,该方法返回QGroupBox是正确的
另一个错误是,如果使用
setWidgetResizable(False)
,内部小部件将收缩,这不美观另一个问题是,不应该设置固定的大小,而应该只设置固定的宽度。如果要设置固定的大小,那么应该使滚动条始终可见
输出:
相关问题 更多 >
编程相关推荐