[英]Add scrollbar to grid layout in 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_())
我想向包含圖標的窗口添加滾動條,但不知道如何操作。
您可以使用QScrollArea
。 將GridLayout
放在Widget
,然后將該Widget
放在ScrollArea
。
注意文檔中的注釋:
使用滾動區域顯示自定義窗口小部件的內容時,重要的是確保將子窗口小部件的大小提示設置為合適的值。 如果將標准QWidget用於子窗口小部件,則可能有必要調用QWidget :: setMinimumSize()以確保在滾動區域內正確顯示窗口小部件的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.