python Qt:主窗口小部件滚动条 [英] python Qt: main widget scroll bar

查看:370
本文介绍了python Qt:主窗口小部件滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们试图将滚动条放在主窗口小部件上,因此,如果用户调整主窗口的大小,则会出现滚动条,并让他上下移动以查看较小窗口小部件外部的子窗口小部件,从而允许其移动左右.

We are trying to put scrollbar on the main widget, so if the user resizes the main window, the scrollbar appears, and let he move up and down to see child widgets that is outside the smaller window widget, allowing it to move right and left.

这是带有滚动条的主窗口小部件的代码.

Here is the code for the main widget with the scroll-bar..

def centralWDG(self,MainWindow):
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")

    self.summaryBox = QtGui.QGroupBox("Project Management Layout")
    self.summaryBox.setMinimumHeight(300)
    self.summaryBox.setMinimumWidth(500)

    self.summaryBoxScroll = QtGui.QScrollArea()
    self.summaryBoxScroll.setFrameStyle(QtGui.QFrame.NoFrame)

    self.summaryBoxTopLayout = QtGui.QVBoxLayout(self.summaryBox)
    self.summaryBoxTopLayout.setContentsMargins(1,1,1,1)
    self.summaryBoxTopLayout.addWidget(self.summaryBoxScroll) 

    self.summaryBoxScroll.setWidget(self.centralwidget)

    self.summaryBoxLayout = QtGui.QFormLayout()
    self.summaryBoxLayout.setSpacing(1)
    self.summaryBoxLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)

    self.summaryBoxLayout = QtGui.QFormLayout(self.centralwidget)
    self.summaryBoxLayout.setSpacing(1)
    self.summaryBoxLayout.setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize)

    self.callchildGUIs()

    MainWindow.setCentralWidget(self.centralwidget)

系统已启动,所有GUI均正常运行,但滚动条未显示,如果我们将窗口调整为很小的尺寸也没关系.那么,这里缺少什么?

The system is started, all GUIs work nicely, but the scroll-bar doesn't show up, it doesn't matter if we resize the windows to very small size. So, what is missing here?

所有评论和建议都将受到高度赞赏.

All comments and suggestions are highly appreciated.

推荐答案

您使用centralWidget(它是QWidget)作为主窗口的中央小部件,滚动区域永远不会添加到窗口中.仅包含中央小部件还不够.

You use centralWidget (which is a QWidget) as the central widget of the main window, the scroll area is never added to the window. Having it contain the central widget is not enough.

pyuic已生成以下代码:

def setupUi(self, MainWindow):
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")

    self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
    self.verticalLayout.setObjectName("verticalLayout")

    self.scrollArea = QtGui.QScrollArea(self.centralwidget)
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setObjectName("scrollArea")
    self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    self.verticalLayout.addWidget(self.scrollArea)

    self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 778, 527))
    self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")

    self.verticalLayout_2 = QtGui.QVBoxLayout(self.scrollAreaWidgetContents)
    self.verticalLayout_2.setObjectName("verticalLayout_2")

    MainWindow.setCentralWidget(self.centralwidget)

滚动区域被添加到中央窗口小部件的布局中,并且具有自己的内容窗口小部件.如果您将控件添加到verticalLayout_2(并且将scrollAreaWidgetContents作为父窗口小部件),则它们将收到滚动条.

The scroll area is added to the layout of the central widget and has its own content widget. If you add controls to verticalLayout_2 (and scrollAreaWidgetContents as the parent widget), they will receive scroll bars.

这篇关于python Qt:主窗口小部件滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆