简体   繁体   English

将滚动条添加到pyqt4中的网格布局

[英]Add scrollbar to grid layout in pyqt4

I have coded the following python program. 我已经编写了以下python程序。 It shows multiple icons in a grid view. 它在网格视图中显示多个图标。

import sys
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore 

class Main(QtGui.QMainWindow):
        def __init__(self, parent = None):
            super(Main, self).__init__(parent)

            self.centralWidget=QWidget()
            scrollArea=QScrollArea()
            scrollArea.setWidgetResizable(True)
            scrollArea.setWidget(self.centralWidget)
            self.setCentralWidget(self.centralWidget)
            w=QGridLayout()

            size=128
            icon=QIcon()
            mode=QIcon.Normal
            state=QIcon.Off
            pixma = QPixmap('a.png') 
            icon.addPixmap(pixma,mode,state)
            positions = [(i,j) for i in range(5) for j in range(4)]
            for position in positions:
                label=QLabel()
                label.setPixmap(icon.pixmap(size,QIcon.Normal,state))
                w.addWidget(label,*position) 

            self.centralWidget.setLayout(w)


a = QApplication(sys.argv) 
q=Main() 
q.show() 
sys.exit(a.exec_())

I want to add a scrollbar to the window containing icons but don't know how. 我想向包含图标的窗口添加滚动条,但不知道如何操作。

You can use a QScrollArea . 您可以使用QScrollArea Put your GridLayout in a Widget and put that Widget in a ScrollArea . GridLayout放在Widget ,然后将该Widget放在ScrollArea

Mind the Notes in the documentation: 注意文档中的注释:

When using a scroll area to display the contents of a custom widget, it is important to ensure that the size hint of the child widget is set to a suitable value. 使用滚动区域显示自定义窗口小部件的内容时,重要的是确保将子窗口小部件的大小提示设置为合适的值。 If a standard QWidget is used for the child widget, it may be necessary to call QWidget::setMinimumSize() to ensure that the contents of the widget are shown correctly within the scroll area. 如果将标准QWidget用于子窗口小部件,则可能有必要调用QWidget :: setMinimumSize()以确保在滚动区域内正确显示窗口小部件的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM