Results 1 to 5 of 5

Thread: override minimumSize function

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default override minimumSize function

    Hello,

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

    Qt Code:
    1. class test : public QLabel {
    2. public:
    3. test(QString s, QWidget* p=0): QLabel(s, p) {}
    4. QSize minimumSizeHint() const {
    5. qDebug() << "minSizeHint";
    6. return QSize(300, 200);
    7. }
    8. QSize minimumSize () const
    9. {
    10. qDebug() << "minSize";
    11. return QSize(300, 200);
    12. }
    13. };
    14.  
    15. int main(int argc, char* argv[])
    16. {
    17. QApplication app(argc, argv);
    18. test l(QString("foo"));
    19. //l.setMinimumSize(200, 200); //works!
    20. l.show();
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

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

    What do I make wrong?
    thanks,
    niko

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: override minimumSize function

    Minimum size is a property of QWidget. The getter QWidget::minimumSize() is not virtual and can therefore not be overridden.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: override minimumSize function

    oh, ok... thanks for that hint.

    but minimumSizeHint is a virtual function; I just chcked the Qt-Source:
    Qt Code:
    1. virtual QSize minimumSizeHint() const;
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QWidget window;
    5. QVBoxLayout layout;
    6. window.setLayout(&layout);
    7. test testLabel(QString("foo"));
    8. layout.addWidget(&testLabel);
    9. window.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    ...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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: override minimumSize function

    Quote Originally Posted by niko View Post
    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.

  5. #5
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: override minimumSize function

    ok, as always, much thanks for your answers...

    niko

Similar Threads

  1. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52
  5. Replies: 25
    Last Post: 15th January 2006, 00:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.