PDA

View Full Version : override minimumSize function



niko
29th October 2006, 21:18
Hello,

I'm trying to override the minimumSize-Function of a widget
(so it can't be resized smaller than this size)



class test : public QLabel {
public:
test(QString s, QWidget* p=0): QLabel(s, p) {}
QSize minimumSizeHint() const {
qDebug() << "minSizeHint";
return QSize(300, 200);
}
QSize minimumSize () const
{
qDebug() << "minSize";
return QSize(300, 200);
}
};

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
test l(QString("foo"));
//l.setMinimumSize(200, 200); //works!
l.show();
return app.exec();
}


The functions minimumSize and minimumSizeHint are never called :(
If I use setMinimumSize it works.

What do I make wrong?
thanks,
niko

jpn
29th October 2006, 21:46
Minimum size is a property of QWidget. The getter QWidget::minimumSize() (http://doc.trolltech.com/4.2/qwidget.html#minimumSize-prop) is not virtual and can therefore not be overridden.

niko
29th October 2006, 22:10
oh, ok... thanks for that hint.

but minimumSizeHint is a virtual function; I just chcked the Qt-Source:

virtual QSize minimumSizeHint() const;

But i found out why minimumSizeHint isn't working: I don't use a Layout in my test - it only works with a layout! (strange??)

my fixed code:

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout layout;
window.setLayout(&layout);
test testLabel(QString("foo"));
layout.addWidget(&testLabel);
window.show();
return app.exec();
}

...is there a reason for this behavior or is it a bug in Qt?
(i guess in real applications it doesn't matter - as layouts always will be used)

niko

wysota
30th October 2006, 00:18
But i found out why minimumSizeHint isn't working: I don't use a Layout in my test - it only works with a layout! (strange??)
...is there a reason for this behavior or is it a bug in Qt?

It's a way Qt is designed - sizeHints are only relevant when a widget is in layout. See the docs:

The default implementation of sizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise.

niko
30th October 2006, 06:28
ok, as always, much thanks for your answers...

niko