PDA

View Full Version : Resizing back the widget



gruszczy
22nd November 2008, 12:36
I have written following piece of code using PyQt.



class QChoices(QWidget):

def __init__(self):
QWidget.__init__(self)
self.setLayout(QVBoxLayout())
policy = QSizePolicy()
policy.setVerticalPolicy(QSizePolicy.Minimum)
self.setSizePolicy(policy)

def addChoice(self, choice):
checkBox = QCheckBox(choice)
checkBox.setCheckState(Qt.Checked)
self.connect(checkBox, SIGNAL('stateChanged(int)'), self.remove)
self.layout().addWidget(checkBox)

def remove(self, state):
if state != Qt.Unchecked:
raise Exception('Choice checked back again!')
self.sender().hide()
self.sender().deleteLater()
self.updateGeometry()


It is supposed to work like attachments in GMail, where you can uncheck a check box to remove an attachment and it disappears. So it works ok, but the problem is, that the widget doesn't resize itself to be smaller again, when one of the checkboxes is hidden. I have set vertical size policy in this widget and in parent widget to Minimum, but it doesn't help. I just can't figure out, how to make it resize itself properly. Could anyone help me with this?

wysota
22nd November 2008, 15:04
Take a look at QLayout::sizeConstraint.

gruszczy
22nd November 2008, 15:21
I tried:


self.layout().setSizeConstraint(QLayout.SetMinimum Size)


but it didn't work.

The only thing that works, is to explicitly resize the widget to the minimalSize, but it doesn't still works, when this widget is inside some other one.

I implemented sizHint and minimalSizeHint, I try to call updateGeometry, but nothing helps. I really don't get this sizing stuff and would really appreciate help on this one.

wysota
22nd November 2008, 15:48
Mimumum means it can be bigger than what is returned by sizeHint (it means that sizeHint is the MINIMUM size that can be set). I think you want "Fixed".

gruszczy
22nd November 2008, 16:10
It works. Nearly perfectly, thanks a lot. It canot be resized now manually (which is a slight imperfection ;-)) - on the other hand I don't really now, how it should behave after manual resize. I don't need it anyway, so I'll just stick to this one.

My another problem is, that I don't really get, how all this resizing works. Could you suggest me some reading? I tried this: http://doc.trolltech.com/4.1/layout.html, but there is not information about this sizeConstraint. I'd really like to dig a bit more into it, especially that I have similar problems with other widgets (but the other way - I want them get bigger, when some internal widget is built).

wysota
22nd November 2008, 16:24
Understanding Qt Layouts (http://chaos.troll.no/~ahanssen/devdays2007/DevDays2007-Layouts.pdf)

gruszczy
23rd November 2008, 10:14
Thanks, I'll take a look.