我想向 QVBoxLayout 添加滚动条
Posted
技术标签:
【中文标题】我想向 QVBoxLayout 添加滚动条【英文标题】:I would like to add a scroll bar to a QVBoxLayout 【发布时间】:2018-09-15 06:08:52 【问题描述】:如何在 PySide2 中向我的 QVBoxLayout 添加滚动条。
self.mainWidget = QtWidgets.QWidget()
self.window.setCentralWidget(self.mainWidget)
self.vertical_layout_main = QtWidgets.QVBoxLayout(self.mainWidget)
scroll = QtWidgets.QScrollArea()
scroll.setWidget(self.vertical_layout_main)
scroll.setFixedHeight(400)
self.vertical_layout_main.addWidget(scroll)
更新:
def populate_lights(self):
self.vertical_layout_lights = QtWidgets.QVBoxLayout(self.mainWidget)
self.vertical_layout_main.addLayout(self.vertical_layout_lights)
for light in self.lights:
horizontal_layout_light = QtWidgets.QVBoxLayout(self.mainWidget)
light_label = QtWidgets.QPushButton(light)
light_label.setCheckable(True)
light_label.toggled.connect(partial(self.light_label_event,light))
horizontal_layout_light.addWidget(light_label)
self.vLayout.addLayout(horizontal_layout_light)
def light_palette_ui(self):
self.vertical_layout_main = QtWidgets.QVBoxLayout(self.mainWidget)
self.scroll_widget = QtWidgets.QWidget()
self.scroll_widget.setLayout(self.vertical_layout_main)
self.scroll = QtWidgets.QScrollArea()
self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(False)
self.scroll.setWidget(self.scroll_widget)
self.vLayout = QtWidgets.QVBoxLayout(self.mainWidget)
self.vLayout.addWidget(self.scroll)
self.populate_lights()
self.window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.window.show()
【问题讨论】:
【参考方案1】:setWidget()
用于将widget添加到QScrollArea,下面我举个例子:
from PySide2 import QtWidgets
class Test:
def __init__(self):
self.window = QtWidgets.QMainWindow()
self.mainWidget = QtWidgets.QWidget()
self.window.setCentralWidget(self.mainWidget)
self.vertical_layout_main = QtWidgets.QVBoxLayout(self.mainWidget)
scroll = QtWidgets.QScrollArea()
content_widget = QtWidgets.QWidget()
scroll.setWidget(content_widget)
scroll.setWidgetResizable(True)
lay = QtWidgets.QVBoxLayout(content_widget)
for l in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
btn = QtWidgets.QPushButton(l)
lay.addWidget(btn)
lay.addStretch()
scroll.setFixedHeight(400)
self.vertical_layout_main.addWidget(scroll)
self.window.show()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
t = Test()
sys.exit(app.exec_())
更新:
from PySide2 import QtCore, QtWidgets
from functools import partial
class Test:
def __init__(self):
self.window = QtWidgets.QMainWindow()
self.mainWidget = QtWidgets.QWidget()
self.lights = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
self.window.setCentralWidget(self.mainWidget)
def light_label_event(self, name, checked):
print(name,checked)
def populate_lights(self):
self.light_layout = QtWidgets.QVBoxLayout(self.scroll_widget)
for light in self.lights:
light_label = QtWidgets.QPushButton(light)
light_label.setCheckable(True)
light_label.toggled.connect(partial(self.light_label_event,light))
self.light_layout.addWidget(light_label)
self.light_layout.addStretch()
def light_palette_ui(self):
self.vertical_layout_main = QtWidgets.QVBoxLayout(self.mainWidget)
self.scroll = QtWidgets.QScrollArea()
self.scroll.setWidgetResizable(True)
self.vertical_layout_main.addWidget(self.scroll)
self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scroll_widget = QtWidgets.QWidget()
self.scroll.setWidget(self.scroll_widget)
self.populate_lights()
self.window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.window.show()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
t = Test()
t.light_palette_ui()
sys.exit(app.exec_())
【讨论】:
谢谢 Eyllanesc,我很感激。发布此问题后,我能够获得一个几乎可以正常工作的示例。但是按钮不会填充滚动区域,而是位于布局中滚动区域的下方。我想发布我的新代码,或者编辑这个问题,或者提出一个新问题,以解决我目前的情况。你有什么建议? @winteralfs 将其添加到您的问题中,我会尽力帮助您,我之前已经告诉过您。 @winteralfs 我已经添加了一个解决方案,不仅复制和执行,而且分析我所做的。以上是关于我想向 QVBoxLayout 添加滚动条的主要内容,如果未能解决你的问题,请参考以下文章