如何在pyqt中创建一个scrollarea
How to create one scrollarea in pyqt
我正在开发一个高度为300的滚动区Widget,在滚动区添加QLabel Widgets,每个QLabel高度为100。添加2个以上QLabel时,滚动区QLabel重叠,滚动bar does not 说明我没有办法拉动滚动条。我想将 QLabel 彼此分开。同时我可以下拉滚动条看到剩余的QLabel
from PyQt4 import QtCore
from PyQt4.QtGui import QScrollArea, QLabel, QVBoxLayout
import sys
from PyQt4 import QtGui
class ScrollArea(QScrollArea):
def __init__(self):
super(ScrollArea, self).__init__()
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
vbox = QVBoxLayout()
self.setLayout(vbox)
for i in range(4):
lb = QLabel('hjkmlasda')
lb.setStyleSheet("""
border-width: 1px;
border-style: solid;
border-color:blue;
""")
lb.setFixedSize(400, 100)
vbox.addWidget(lb)
vbox.addStretch(1)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
h = QtGui.QHBoxLayout()
h.addWidget(ScrollArea())
self.setLayout(h)
self.setGeometry(100, 100, 1000, 500)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在QScrollArea中不用设置布局而是设置一个widget,那么如何放多个widget呢?那么,您必须使用 QWidget 作为容器并通过分配给容器的布局来设置小部件,同时启用 widgetResizable 属性:
class ScrollArea(QtGui.QScrollArea):
def __init__(self, parent=None):
super(ScrollArea, self).__init__(parent)
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setWidgetResizable(True)
container = QtGui.QWidget()
self.setWidget(container)
vbox = QtGui.QVBoxLayout(container)
# vbox.setSpacing(0)
for i in range(4):
lb = QtGui.QLabel("hjkmlasda")
lb.setStyleSheet(
"""
border-width: 1px;
border-style: solid;
border-color:blue;
"""
)
lb.setFixedSize(400, 100)
vbox.addWidget(lb)
您应该为 QScrollArea 设置一个可调整大小的小部件并将布局放置在该小部件上。编辑构造函数:
class ScrollArea(QScrollArea):
def __init__(self):
super(ScrollArea, self).__init__()
w = QWidget()
self.setWidget(w)
self.setWidgetResizable(True)
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
vbox = QVBoxLayout(w)
. . .
我正在开发一个高度为300的滚动区Widget,在滚动区添加QLabel Widgets,每个QLabel高度为100。添加2个以上QLabel时,滚动区QLabel重叠,滚动bar does not 说明我没有办法拉动滚动条。我想将 QLabel 彼此分开。同时我可以下拉滚动条看到剩余的QLabel
from PyQt4 import QtCore
from PyQt4.QtGui import QScrollArea, QLabel, QVBoxLayout
import sys
from PyQt4 import QtGui
class ScrollArea(QScrollArea):
def __init__(self):
super(ScrollArea, self).__init__()
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
vbox = QVBoxLayout()
self.setLayout(vbox)
for i in range(4):
lb = QLabel('hjkmlasda')
lb.setStyleSheet("""
border-width: 1px;
border-style: solid;
border-color:blue;
""")
lb.setFixedSize(400, 100)
vbox.addWidget(lb)
vbox.addStretch(1)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
h = QtGui.QHBoxLayout()
h.addWidget(ScrollArea())
self.setLayout(h)
self.setGeometry(100, 100, 1000, 500)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在QScrollArea中不用设置布局而是设置一个widget,那么如何放多个widget呢?那么,您必须使用 QWidget 作为容器并通过分配给容器的布局来设置小部件,同时启用 widgetResizable 属性:
class ScrollArea(QtGui.QScrollArea):
def __init__(self, parent=None):
super(ScrollArea, self).__init__(parent)
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setWidgetResizable(True)
container = QtGui.QWidget()
self.setWidget(container)
vbox = QtGui.QVBoxLayout(container)
# vbox.setSpacing(0)
for i in range(4):
lb = QtGui.QLabel("hjkmlasda")
lb.setStyleSheet(
"""
border-width: 1px;
border-style: solid;
border-color:blue;
"""
)
lb.setFixedSize(400, 100)
vbox.addWidget(lb)
您应该为 QScrollArea 设置一个可调整大小的小部件并将布局放置在该小部件上。编辑构造函数:
class ScrollArea(QScrollArea):
def __init__(self):
super(ScrollArea, self).__init__()
w = QWidget()
self.setWidget(w)
self.setWidgetResizable(True)
self.setFixedSize(500, 300)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
vbox = QVBoxLayout(w)
. . .