[PyQt] Problem with alignment in QVBoxLayout

John Posner jjpplex at gmail.com
Fri Feb 12 18:04:23 GMT 2010


This PyQt 4.6.2 program:

#--------------
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Btn(QPushButton):
    def __init__(self, text):
        super(Btn, self).__init__(text)
        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

app = QApplication([])

frm = QFrame()
frm.setMinimumSize(400, 300)

lay = QVBoxLayout()
frm.setLayout(lay)
lay.setAlignment(Qt.AlignRight)
lay.addWidget(Btn("one"))
lay.addWidget(Btn("two two two two two"))

frm.show()
app.exec_()
#--------------

... produces a window like this:

+----------------------------------------------------------+

                                      +---------+
                                      |   one   |
                                      +---------+
                                      +-------------------+
                                      |two two two two two|
                                      +-------------------+

+----------------------------------------------------------+


But I *want* to produce a window like this:

+----------------------------------------------------------+

                                                +---------+
                                                |   one   |
                                                +---------+
                                      +-------------------+
                                      |two two two two two|
                                      +-------------------+

+----------------------------------------------------------+

Can I change this code to make this QVBoxLayout produce the desired result?

Note 1: This code change makes no difference in the window:

  lay.addWidget(Btn("one"), Qt.AlignRight)

Note 2: Using this QGridLayout *does* achieve the desired result:

  lay = QGridLayout()
  frm.setLayout(lay)
  lay.addWidget(Btn("one"), 0, 0, Qt.AlignRight)
  lay.addWidget(Btn("two two two two two"), 1, 0, Qt.AlignRight)


Tx,
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100212/7a9920b3/attachment-0001.html>


More information about the PyQt mailing list