我很难理解如何在PyQt5中使用滚动条。我有一个任意的布局来试验,这只是一些颜色块。我希望颜色的布局具有最小尺寸,并且当窗口小于该尺寸时,滚动条出现(或者滚动条始终存在)。我找到了这个例子:
并将其更改为与PyQt5一起使用:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QScrollArea, QVBoxLayout, QWidget
class myDialog(QDialog):
_buttons = 0
def __init__(self, parent=None):
super(myDialog, self).__init__(parent)
self.pushButton = QPushButton(self)
self.pushButton.setText("Add Button!")
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.scrollArea = QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget(self.scrollArea)
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 380, 247))
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayout = QVBoxLayout(self)
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.scrollArea)
self.verticalLayoutScroll = QVBoxLayout(self.scrollAreaWidgetContents)
'''
verticalLayoutScroll is a child of scrollAreaWidgetContents
scrollAreaWidgetContents is a child of scrollArea
scrollArea is child of self
verticalLayout is also a child of self has the widgets pushButton and scrollArea
'''
@QtCore.pyqtSlot()
def on_pushButton_clicked(self):
self._buttons += 1
pustButtonName = u"Button {0}".format(self._buttons)
pushButton = QPushButton(self.scrollAreaWidgetContents)
pushButton.setText(pustButtonName)
self.verticalLayoutScroll.addWidget(pushButton)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setApplicationName('myDialog')
main = myDialog()
main.show()
sys.exit(app.exec_())
这是我的代码:
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 15 07:37:28 2018
@author: Erik
"""
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QSlider, QPushButton,
QMainWindow, QAction, qApp,
QLCDNumber, QGridLayout, QLabel, QLineEdit, QVBoxLayout, QHBoxLayout,
QSizePolicy, QScrollArea, QLayout)
from PyQt5.QtGui import QIcon, QColor, QPalette
from PyQt5.QtCore import Qt, QSize, QRect
class Color(QWidget):
def __init__(self, color, *args, **kwargs):
super(Color, self).__init__(*args, **kwargs)
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
class Color2(QWidget):
def __init__(self, color, *args, **kwargs):
super(Color2, self).__init__(*args, **kwargs)
self.setAutoFillBackground(True)
palette = self.palette()
palette.setColor(QPalette.Window, QColor(color))
self.setPalette(palette)
def sizeHint(self):
return QSize(150,150)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("My Awesome App")
size = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
layout = QVBoxLayout()
layout2 = QVBoxLayout()
brw = Color2('brown')
brw.setSizePolicy(size)
layout2.addWidget(brw)
layout2.addWidget(Color('black'))
layout2.setStretch(1,2)
layout2.addWidget(Color('cyan'))
layout2.setStretch(2, 1)
layout.addWidget(Color('red'))
layout.addWidget(Color('blue'))
layout.addWidget(Color('green'))
self.scrollArea = QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget(self.scrollArea)
self.scrollAreaWidgetContents.setGeometry(QRect(0, 0, 600, 500))
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollAreaWidgetContents.setMinimumSize(QSize(500, 500))
self.scrollLayout = QVBoxLayout(self.scrollAreaWidgetContents)
#self.scrollLayout.setSizeConstraint(QLayout.SetMinimumSize) #2
self.mainLayout = QHBoxLayout()
self.mainLayout.addWidget(Color('purple'))
self.mainLayout.addWidget(Color('yellow'))
self.mainLayout.addWidget(Color('orange'))
self.mainLayout.addLayout(layout2)
self.mainLayout.addLayout(layout)
self.scrollLayout.addLayout(self.mainLayout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
我几乎看不到scrollArea中的颜色。在上面的第一个代码块中,我不理解我在注释块中描述的必要的继承结构。我是否需要扩展布局类来设置最小尺寸? https://doc.qt.io/qt-5/qlayoutitem.html#minimumSize https://doc.qt.io/qt-5/qscrollarea.html#details的“大小提示和布局”部分如何相关? 谢谢。
答案 0 :(得分:1)
问题是您尚未将QScrollArea
建立为centralWidget。
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("My Awesome App")
size = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
layout = QVBoxLayout()
layout2 = QVBoxLayout()
brw = Color2('brown')
brw.setSizePolicy(size)
layout2.addWidget(brw)
layout2.addWidget(Color('black'))
layout2.setStretch(1,2)
layout2.addWidget(Color('cyan'))
layout2.setStretch(2, 1)
layout.addWidget(Color('red'))
layout.addWidget(Color('blue'))
layout.addWidget(Color('green'))
self.scrollArea = QScrollArea()
# set CentralWidget
self.setCentralWidget(self.scrollArea)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget(self.scrollArea)
self.scrollAreaWidgetContents.setGeometry(QRect(0, 0, 600, 500))
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollAreaWidgetContents.setMinimumSize(QSize(500, 500))
self.scrollLayout = QVBoxLayout(self.scrollAreaWidgetContents)
#self.scrollLayout.setSizeConstraint(QLayout.SetMinimumSize) #2
self.mainLayout = QHBoxLayout()
self.mainLayout.addWidget(Color('purple'))
self.mainLayout.addWidget(Color('yellow'))
self.mainLayout.addWidget(Color('orange'))
self.mainLayout.addLayout(layout2)
self.mainLayout.addLayout(layout)
self.scrollLayout.addLayout(self.mainLayout)
self.show()