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

时间:2015-05-20 06:13:43

标签: python qt pyqt4

我编写了以下python程序。它在网格视图中显示多个图标。

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_())

我想在包含图标的窗口中添加滚动条但不知道如何。

1 个答案:

答案 0 :(得分:1)

您可以使用QScrollArea。 将GridLayout放入Widget并将Widget放入ScrollArea

记住文档中的注释:

  

使用滚动区域显示自定义小部件的内容时,   确保子窗口小部件的大小提示很重要   设置为合适的值。如果孩子使用标准的QWidget   小部件,可能需要调用 QWidget :: setMinimumSize()   确保小部件的内容在内部正确显示   滚动区域。

相关问题