Qscrollarea 不显示滚动条并且布局弹出不合适

发布于 2024-10-07 21:12:06 字数 1772 浏览 0 评论 0原文

我昨晚花了很多时间试图让这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里的微风 2024-10-14 21:12:06

这看起来是错误的:

self.scrollArea = QtGui.QScrollArea() 
self.scrollArea.setWidget( self.lowerWidget )           

layout.addWidget( self.lowerWidget )

您将 lowerWidget 添加到滚动区域,只是为了将其添加到下一步的布局中,这会从滚动区域中删除 lowerWidget 并重新设置其顶级小部件的父级。
您必须将滚动区域添加到布局中:

self.scrollArea = QtGui.QScrollArea() 
self.scrollArea.setWidget( self.lowerWidget )       

layout.addWidget( self.scrollArea )

This looks wrong:

self.scrollArea = QtGui.QScrollArea() 
self.scrollArea.setWidget( self.lowerWidget )           

layout.addWidget( self.lowerWidget )

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:

self.scrollArea = QtGui.QScrollArea() 
self.scrollArea.setWidget( self.lowerWidget )       

layout.addWidget( self.scrollArea )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文