Results 1 to 7 of 7

Thread: resizing widgets depending on a main widget size

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.