如何使用pyqt4将小部件添加到网格布局的中心

How to add widget to center of the gridlayout using with pyqt4

在我的示例程序中,我想将小部件添加到 gridlayout.I 使用的对齐方法的中心,但它不是 working.Can 任何人请帮助我如何调整我的小部件在中心网格布局。提前谢谢你。

下面是我的示例代码:

import sys
from PyQt4 import QtGui,QtCore
class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    def initUI(self):
        self.grid = QtGui.QGridLayout(self)
        self.title_hbox = QtGui.QHBoxLayout()
        self.title_hbox.addStretch()
        self.close_pushbtn = QtGui.QPushButton("Close")
        self.title_hbox.addWidget(self.close_pushbtn)
        self.grid.addLayout(self.title_hbox,0,0)
        self.line = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
        self.grid.addWidget(self.line,1,0)
        self.order_hbox = QtGui.QHBoxLayout()
        vbox = QtGui.QVBoxLayout()
        label = QtGui.QLabel("address ")
        vbox.addWidget(label)
        hbox=QtGui.QHBoxLayout()
        le =QtGui.QLabel("scroll area")
        hbox.addWidget(le)
        self.order_hbox.addLayout(vbox)
        self.order_hbox.addLayout(hbox)
        self.grid.addLayout(self.order_hbox,2,0)
        self.addhbox = QtGui.QHBoxLayout()
        self.add_btn = QtGui.QPushButton("Add") #@ Add button i want to adjust the center of the gridlayout (its tacking whole length..)
        self.addhbox.addWidget(self.add_btn,QtCore.Qt.AlignCenter)
        self.grid.addLayout(self.addhbox,3,0)
        self.resize(800, 300)
        self.show()
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我的添加按钮占据了整个宽度,但我想调整小部件的中间

您必须更改按钮的 QSizePolicy,使其不会水平扩展。

不要滥用自己,这用于 "self" 保留参考,但在布局的情况下,您很少会访问它。另一方面,为了保持顺序,我首先声明了小部件。

最后我删除了一些不需要的布局。

def initUI(self):
    self.close_pushbtn = QtGui.QPushButton(
        text="Close",
        sizePolicy=QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum)
    )
    self.line = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
    self.add_btn = QtGui.QPushButton(
        text="Add",
        sizePolicy=QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Maximum)
    )
    label = QtGui.QLabel(text="address ")
    le = QtGui.QLabel(text="scroll area")

    title_hbox = QtGui.QHBoxLayout()
    title_hbox.addWidget(self.close_pushbtn, alignment=QtCore.Qt.AlignRight)

    order_hbox = QtGui.QHBoxLayout()
    order_hbox.addWidget(label)
    order_hbox.addWidget(le)

    addhbox = QtGui.QHBoxLayout()
    addhbox.addWidget(self.add_btn, alignment=QtCore.Qt.AlignCenter)

    grid = QtGui.QVBoxLayout(self)
    grid.addLayout(title_hbox)
    grid.addWidget(self.line)
    grid.addLayout(order_hbox)
    grid.addLayout(addhbox)

    self.resize(800, 300)
    self.show()

另一种选择是在每一侧使用拉伸:

def initUI(self):
    self.close_pushbtn = QtGui.QPushButton(text="Close")
    self.line = QtGui.QFrame(frameShape=QtGui.QFrame.HLine)
    self.add_btn = QtGui.QPushButton(text="Add")
    label = QtGui.QLabel(text="address ")
    le = QtGui.QLabel(text="scroll area")

    title_hbox = QtGui.QHBoxLayout()
    title_hbox.addStretch()
    title_hbox.addWidget(self.close_pushbtn)

    order_hbox = QtGui.QHBoxLayout()
    order_hbox.addWidget(label)
    order_hbox.addWidget(le)

    addhbox = QtGui.QHBoxLayout()
    addhbox.addStretch()
    addhbox.addWidget(self.add_btn)
    addhbox.addStretch()

    grid = QtGui.QVBoxLayout(self)
    grid.addLayout(title_hbox)
    grid.addWidget(self.line)
    grid.addLayout(order_hbox)
    grid.addLayout(addhbox)

    self.resize(800, 300)
    self.show()