[Qt-interest] Problems with alignment in QVBoxLayout

John Posner jjposner at optimum.net
Tue Feb 16 19:36:21 CET 2010


[ I posted this to the PyQt list on Feb 12, but got no response. View 
this message with a monospace font. ]


This PyQt 4.6.2 (qVersion = 4.5.3) 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




More information about the Qt-interest-old mailing list