我想给 QVBoxLayout 添加一个滚动条
I would like to add a scroll bar to a QVBoxLayout
如何在 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()
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_())
如何在 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()
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_())