如何对齐QHBoxLayout和QVBoxLayout的布局? [英] How to align the layouts QHBoxLayout and QVBoxLayout?

查看:1222
本文介绍了如何对齐QHBoxLayout和QVBoxLayout的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为我的窗口进行以下布局:

I want to do this layout for my window:

所以我尝试创建一个QHBoxLayout布局以放置3个按钮,并将其添加到QVBoxLayout:

So I tried to create a QHBoxLayout layout to put the 3 buttons, and add it to the QVBoxLayout:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):

        title = QtGui.QLabel( 'Title' )
        author = QtGui.QLabel( 'Author' )
        other = QtGui.QLabel( 'Other' )

        titleEdit = QtGui.QLineEdit()

        horizontalLayout = QtGui.QHBoxLayout( self )
        horizontalLayout.addWidget( title )
        horizontalLayout.addWidget( author )
        horizontalLayout.addWidget( other )

        verticalLayout = QtGui.QVBoxLayout( self )
        verticalLayout.addWidget( titleEdit )
        verticalLayout.addWidget( horizontalLayout )

        self.setLayout( verticalLayout )

        self.setGeometry( 300, 300, 350, 300 )
        self.setWindowTitle( 'Review' )
        self.show()

def main():

    app = QtGui.QApplication( sys.argv )
    ex = Example()
    sys.exit( app.exec_() )


if __name__ == '__main__':
    main()

但是它不接受其他布局:

But it is not accepting another layout:

verticalLayout.addWidget( horizontalLayout )
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Qt.Alignment = 0): argument 1 has unexpected type 'QHBoxLayout'

如何进行此对齐?

使用addLayout()通过@ GM 注释,我在控制台上收到了以下警告:

By @G.M. comment, using addLayout() I got this warning on the console:

QLayout: Attempting to add QLayout "" to Example "", which already has a layout
QLayout::addChildLayout: layout "" already has a parent
QWidget::setLayout: Attempting to set QLayout "" on Example "", which already has a layout

但是现在显示的窗口没有编辑框:

But now the window was displayed without the edit box:

推荐答案

在更新中显示的问题是由于您必须以自己的父母身份而产生的,并将其放置在该小部件中,一个简单的解决方案是将其更改为:

The problem you show in your update is generated because you have to self as your parent, and this is placed in that widget, a simple solution is change to:

horizontalLayout = QtGui.QHBoxLayout()

完整代码:

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self)
        title = QtGui.QLabel( 'Title' )
        author = QtGui.QLabel( 'Author' )
        other = QtGui.QLabel( 'Other' )

        titleEdit = QtGui.QLineEdit()

        horizontalLayout = QtGui.QHBoxLayout()
        horizontalLayout.addWidget( title )
        horizontalLayout.addWidget( author )
        horizontalLayout.addWidget( other )

        verticalLayout = QtGui.QVBoxLayout( self )
        verticalLayout.addLayout( horizontalLayout )

        verticalLayout.addWidget( titleEdit )


        self.setLayout( verticalLayout )

        self.setGeometry( 300, 300, 350, 300 )
        self.setWindowTitle( 'Review' )
        self.show()

def main():

    app = QtGui.QApplication( sys.argv )
    ex = Example()
    sys.exit( app.exec_() )

另一个问题是您在谈论按钮,除了该图显示了几乎是正方形尺寸的小部件外,我认为如果要获取它,则应使用QTextEdit.

Another question is that you talk about buttons, in addition that the graph shows a widget of almost square dimensions, I think if you want to get it you should use QTextEdit.

完整代码:

#!/usr/bin/python

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):

        title = QtGui.QPushButton( 'Title' )
        author = QtGui.QPushButton( 'Author' )
        other = QtGui.QPushButton( 'Other' )

        titleEdit = QtGui.QTextEdit()

        horizontalLayout = QtGui.QHBoxLayout()
        horizontalLayout.addWidget( title )
        horizontalLayout.addWidget( author )
        horizontalLayout.addWidget( other )

        verticalLayout = QtGui.QVBoxLayout( self )
        verticalLayout.addLayout( horizontalLayout )

        verticalLayout.addWidget( titleEdit )


        self.setLayout( verticalLayout )

        self.setGeometry( 300, 300, 350, 300 )
        self.setWindowTitle( 'Review' )
        self.show()

def main():

    app = QtGui.QApplication( sys.argv )
    ex = Example()
    sys.exit( app.exec_() )


if __name__ == '__main__':
    main()

这篇关于如何对齐QHBoxLayout和QVBoxLayout的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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