gpt4 book ai didi

python - 我想向 QVBoxLayout 添加滚动条

转载 作者:行者123 更新时间:2023-11-30 22:07:55 24 4
gpt4 key购买 nike

如何向 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() 用于将小部件添加到 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_())

关于python - 我想向 QVBoxLayout 添加滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52342123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com