Results 1 to 7 of 7

Thread: resizing widgets depending on a main widget size

  1. #1
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default resizing widgets depending on a main widget size

    I'm trying to make a custom widget get it's size (fixed size) set depending on the size of a stacked widget.

    I pick up the resize in resizeEvent of the main widget, and emit a signal to tell the other widget to resize. The problem is that when the custom widget has large fixed size, and I try to make the window smaller, it doesn't allow to scale/resize down properly. It works if I scale down several times, but that isn't a good solution.

    I've figured out that I really should set the size before the resizeevent, as when that is called, the layout is already done. I just don't figure out what event it should be done in.

    I tried implementing the following, as an small example.

    custom.h:
    Qt Code:
    1. class QCustomMainPage : QWidget
    2. {
    3. public:
    4. QCustomMainPage(QWidget *parent)
    5. protected:
    6. void resizeEvent ( QResizeEvent *re );
    7. signals:
    8. void sizeChange(QSize size);
    9. };
    10.  
    11. class QCustomResizeWidget : QFrame
    12. {
    13. public:
    14. QCustomResizeWidget (QWidget *parent)
    15. public slots:
    16. void sizeChangeSlot(QSize size);
    17. protected:
    18. QSize sizeHint(void) const;
    19. QSize minimumSizeHint(void) const;
    20. QSize currentsize;
    21. };
    To copy to clipboard, switch view to plain text mode 

    custom.cpp
    Qt Code:
    1. QCustomResizeWidget::QCustomResizeWidget(QWidget *parent):QFrame (parent){
    2. QLabel *label1 = new QLabel("label1");
    3. QLabel *label2 = new QLabel("label2");
    4. QVBoxLayout *clayout = new QVBoxLayout(this);
    5. clayout->addWidget(label1);
    6. clayout->addWidget(label2);
    7. }
    8.  
    9. void QCustomResizeWidgetsize::ChangeSlot(QSize size){
    10. currentsize = QSize(size.width()/10, size.width()/10); // keep it square
    11. this->setFixedSize(currentsize);
    12. }
    13. QSize QCustomResizeWidgetsize::sizeHint(void) const{
    14. return this->currentsize;
    15. }
    16. QSize minimumSizeHint(void) const{
    17. // return this->currentsize;
    18. return QSize(1,1); // allow widget to resize down to 1,1 (to get this partially working)
    19. }
    20.  
    21. QCustomMainPage::QCustomMainPage(QWidget *parent) : QWidget(parent){
    22. QCustomResizeWidget *customwidget1 = new QCustomResizeWidget;
    23. connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
    24. QCustomResizeWidget *customwidget2 = new QCustomResizeWidget;
    25. connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
    26. QCustomResizeWidget *customwidget3 = new QCustomResizeWidget;
    27. connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
    28. QCustomResizeWidget *customwidget4 = new QCustomResizeWidget;
    29. connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
    30.  
    31. QGridLayout *grid = new QGridLayout(this);
    32. grid->addWidget(customwidget1, 0,0);
    33. grid->addWidget(customwidget1, 0,1);
    34. grid->addWidget(customwidget1, 1,0);
    35. grid->addWidget(customwidget1, 1,1);
    36. }
    37.  
    38. QCustomMainPage::resizeEvent(QResizeEvent *re ){
    39. emit sizeChange(re->size());
    40. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by luf; 16th June 2009 at 11:45.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: resizing widgets depending on a main widget size

    Hmm... could you show an example of how would you use such widgets?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: resizing widgets depending on a main widget size

    It's to keep a widget the same size throughout an application depending on the main window size.

    The widget might contain anything (Pixmap, text, other widgets, but should always be the same size).

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: resizing widgets depending on a main widget size

    But you don't need any special treatment to achieve that... Just set the size policies right.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    luf (16th June 2009)

  6. #5
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: resizing widgets depending on a main widget size

    I got really carried away into the sizehints, custom x,y,z and didn't see the obvious...

    I forgot to add stretch in some of the layouts to the widgets, meaning that the layout would use the minimum possible size.

    Thanks wysota!

    For others experiencing similar issues:
    setMaximiumSize on the widget + QSizePolicy::MinimumExpanding should make sure the widget is the correct size, and is possible to shrink.
    make sure all widgets have stretch added to them in layouts, so they get the size you expect them to have.

    This should work:

    Qt Code:
    1. class QCustomMainPage : QWidget
    2. {
    3. public:
    4. QCustomMainPage(QWidget *parent)
    5. protected:
    6. void resizeEvent ( QResizeEvent *re );
    7. signals:
    8. void sizeChange(QSize size);
    9. };
    10.  
    11. class QCustomResizeWidget : QFrame
    12. {
    13. public:
    14. QCustomResizeWidget (QWidget *parent)
    15. public slots:
    16. void sizeChangeSlot(QSize size);
    17. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QCustomResizeWidget::QCustomResizeWidget(QWidget *parent):QFrame (parent){
    2. QLabel *label1 = new QLabel("label1");
    3. QLabel *label2 = new QLabel("label2");
    4. QVBoxLayout *clayout = new QVBoxLayout(this);
    5. clayout->addWidget(label1);
    6. clayout->addWidget(label2);
    7. }
    8.  
    9. void QCustomResizeWidgetsize::ChangeSlot(QSize size){
    10. this->setFixedSize(QSize(size.width()/10, size.height()/10));
    11. }
    12.  
    13. QCustomMainPage::QCustomMainPage(QWidget *parent) : QWidget(parent){
    14. QCustomResizeWidget *customwidget1 = new QCustomResizeWidget;
    15. connect(this, SIGNAL(sizeChange(QSize)), customwidget1, SLOT(ChangeSlot(QSize)));
    16. QCustomResizeWidget *customwidget2 = new QCustomResizeWidget;
    17. connect(this, SIGNAL(sizeChange(QSize)), customwidget2, SLOT(ChangeSlot(QSize)));
    18. QCustomResizeWidget *customwidget3 = new QCustomResizeWidget;
    19. connect(this, SIGNAL(sizeChange(QSize)), customwidget3, SLOT(ChangeSlot(QSize)));
    20. QCustomResizeWidget *customwidget4 = new QCustomResizeWidget;
    21. connect(this, SIGNAL(sizeChange(QSize)), customwidget4, SLOT(ChangeSlot(QSize)));
    22. QCustomResizeWidget *customwidget5 = new QCustomResizeWidget;
    23. connect(this, SIGNAL(sizeChange(QSize)), customwidget5, SLOT(ChangeSlot(QSize)));
    24. QCustomResizeWidget *customwidget6 = new QCustomResizeWidget;
    25. connect(this, SIGNAL(sizeChange(QSize)), customwidget6, SLOT(ChangeSlot(QSize)));
    26.  
    27. QHBoxLayout *hbox = new QHBoxLayout();
    28. hboxlayout->addStretch(1); // push widgets to the right
    29. hbox->addWidget(customwidget5, 1) // remember stretch
    30. hbox->addWidget(customwidget6, 1) // remember stretch
    31. QGridLayout *grid = new QGridLayout();
    32. grid->addWidget(customwidget1, 0,0);
    33. grid->addWidget(customwidget2, 0,1);
    34. grid->addWidget(customwidget3, 1,0);
    35. grid->addWidget(customwidget4, 1,1);
    36. QVBoxLayout *mainlayout = QVBoxLayout(this);
    37. mainlayout->addLayout(hbox);
    38. mainlayout->addLayout(grid);
    39. }
    40.  
    41. QCustomMainPage::resizeEvent(QResizeEvent *re ){
    42. emit sizeChange(re->size());
    43. }
    To copy to clipboard, switch view to plain text mode 

    (to improve speed of application, the calculation of widget size should be done before emitting the sizeChange signal).

  7. #6
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: resizing widgets depending on a main widget size

    I have a similar problem. Is there a way to find the size of QCustomResizeWidget? Perhaps, I should describe my situation first.

    I have an application similar to Windows Explorer. In particular, when a User can view icons/images as "Thumbnails". The difference in my application, is the thumbnail images are not a fixed size. The thumbnail size is determined by calculations that require the width of the parent widget (ie. the Main Window).

    I was able to get the correct size in certain situations. I tried using any function that returned QSize or QRect and all of them returned a funny number. It looks like the dimensions get changed when I add a layout to my Main Window (I use a GridLayout). After reading a bit of the Qt documentation, adding a layout instantly shrinks the size of the Main Window.

    After reading this thread, I tried playing with the Size Policies and I was still unable to get the value I was looking for. Is there something else I should be doing, or am I choosing the wrong flags? I've tried all the flags, and I'm still not getting the correct dimensions.

    Any help will be greatly appreciated.
    Last edited by kyue; 9th October 2009 at 21:14.

  8. #7
    Join Date
    Oct 2009
    Posts
    70
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: resizing widgets depending on a main widget size


Similar Threads

  1. Replies: 8
    Last Post: 7th May 2009, 14:38
  2. Replies: 2
    Last Post: 23rd March 2009, 17:26
  3. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  4. Replies: 4
    Last Post: 10th March 2007, 18:01
  5. Replies: 4
    Last Post: 10th December 2006, 09:04

Tags for this Thread

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.