Qscrollarea 不显示滚动条并且布局弹出不合适
我昨晚花了很多时间试图让这个 QScrollArea 工作,但没有运气。我想做的是在菜单下方添加顶部水平菜单布局和可滚动垂直内容布局。滚动条不可见,一旦我向其中添加新元素(通过单击菜单按钮之一),内容布局就会弹出。
请帮帮我。 :)
问候, 拉斯埃里克
import sys
from PyQt4 import QtCore, QtGui, Qt
class MainWindow( QtGui.QMainWindow ):
def __init__( self ):
QtGui.QMainWindow.__init__( self )
self.centralWidget = QtGui.QWidget()
self.setCentralWidget( self.centralWidget )
#Main Layout
layout = QtGui.QVBoxLayout()
layout.setSpacing( 0 )
self.centralWidget.setLayout( layout )
#Top Menu Layout
hLayout = QtGui.QHBoxLayout()
layout.addLayout( hLayout )
i = 0
while i < 5:
addContent = QtGui.QPushButton( 'Add Content' )
hLayout.addWidget( addContent )
self.connect(addContent, QtCore.SIGNAL('clicked()'), self.addContent)
i += 1
#Content Layout
self.lowerWidget = QtGui.QWidget()
#self.lowerWidget.setMaximumSize( Qt.QSize(150, 250) )
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setWidget( self.lowerWidget )
layout.addWidget( self.lowerWidget )
self.vLayout = QtGui.QVBoxLayout()
self.lowerWidget.setLayout( self.vLayout )
i = 0
while i < 25:
label = QtGui.QLabel( 'Content' )
self.vLayout.addWidget( label )
i += 1
def addContent(self):
label = QtGui.QLabel( 'Content' )
self.vLayout.addWidget( label )
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
I spent last night trying to get this QScrollArea to work without any luck. What I am trying to do is to add a top horizontal menu layout and a scrollable vertical content layout beneath the menu. The scrollbar is not visible and the content layout pops out of place as soon as I add new elements to it (by click one of the menu buttons).
Please help me out. :)
Regards,
Lars Erik
import sys
from PyQt4 import QtCore, QtGui, Qt
class MainWindow( QtGui.QMainWindow ):
def __init__( self ):
QtGui.QMainWindow.__init__( self )
self.centralWidget = QtGui.QWidget()
self.setCentralWidget( self.centralWidget )
#Main Layout
layout = QtGui.QVBoxLayout()
layout.setSpacing( 0 )
self.centralWidget.setLayout( layout )
#Top Menu Layout
hLayout = QtGui.QHBoxLayout()
layout.addLayout( hLayout )
i = 0
while i < 5:
addContent = QtGui.QPushButton( 'Add Content' )
hLayout.addWidget( addContent )
self.connect(addContent, QtCore.SIGNAL('clicked()'), self.addContent)
i += 1
#Content Layout
self.lowerWidget = QtGui.QWidget()
#self.lowerWidget.setMaximumSize( Qt.QSize(150, 250) )
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setWidget( self.lowerWidget )
layout.addWidget( self.lowerWidget )
self.vLayout = QtGui.QVBoxLayout()
self.lowerWidget.setLayout( self.vLayout )
i = 0
while i < 25:
label = QtGui.QLabel( 'Content' )
self.vLayout.addWidget( label )
i += 1
def addContent(self):
label = QtGui.QLabel( 'Content' )
self.vLayout.addWidget( label )
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来是错误的:
您将 lowerWidget 添加到滚动区域,只是为了将其添加到下一步的布局中,这会从滚动区域中删除 lowerWidget 并重新设置其顶级小部件的父级。
您必须将滚动区域添加到布局中:
This looks wrong:
You add lowerWidget to the scrollarea, just to add it to the layout in the next step, which removes lowerWidget from the scrollarea and reparent its top level widget.
You must add the scrollarea to the layout: