Results 1 to 3 of 3

Thread: How can i achieve an auto-resizing QStackedWidget?

  1. #1
    Join Date
    Nov 2008
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How can i achieve an auto-resizing QStackedWidget?

    Whenever a widget on a different index is shown (i.e. whenever currentChanged(int) is emitted), i would like for the stackedwidget to resize itself according to the minimize size of the current widget. By default, QStackedWidget uses the biggest widget in the stack to determine the size of the stackedwidget. I have tried multiple attempts, ranging from size policies, size constraints to size hints, both from the stackedwidget itself to even the child widgets in hopes of achieving the desired function. Unfortunately, nothing i have tried worked.

    Assume at least the following structure:
    Qt Code:
    1. QStackedWidget * stackedwidget = new QStackedWidget;
    2.  
    3. stackedwidget->addWidget(new QWidget);
    4. stackedwidget->addWidget(new QWidget);
    5.  
    6. stackedwidget->widget(0)->setMinimumHeight(100);
    7. stackedwidget->widget(1)->setMinimumHeight(200);
    To copy to clipboard, switch view to plain text mode 
    What would i have to do in order to achieve my goal? I am out of ideas.

  2. #2
    Join Date
    Nov 2008
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can i achieve an auto-resizing QStackedWidget?

    So i have decided to create my own simple "stacked widget" and it seems to work fine. For anyone interested:

    Definition:
    Qt Code:
    1. #include <QList>
    2. #include <QWidget>
    3.  
    4. class StackedWidget : public QWidget
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. StackedWidget(QWidget * = 0, Qt::WindowFlags = 0);
    10.  
    11. void addWidget(QWidget *);
    12. int count();
    13. QWidget * currentWidget();
    14.  
    15. void setAutoResize(bool yes)
    16. { auto_resize = yes; }
    17.  
    18. QSize sizeHint();
    19.  
    20. protected:
    21. void showCurrentWidget();
    22.  
    23. private:
    24. bool auto_resize;
    25. int curr_index;
    26. QList<QWidget *> widgets;
    27.  
    28. public slots:
    29. void setCurrentIndex(int);
    30. };
    To copy to clipboard, switch view to plain text mode 

    Implementation:
    Qt Code:
    1. #include <QVBoxLayout>
    2.  
    3. StackedWidget::StackedWidget(QWidget * parent, Qt::WindowFlags f)
    4. : QWidget(parent, f),
    5. curr_index(0)
    6. {
    7. QVBoxLayout * layout = new QVBoxLayout(this);
    8. }
    9.  
    10. int StackedWidget::count()
    11. { return widgets.count(); }
    12.  
    13. void StackedWidget::addWidget(QWidget * w)
    14. {
    15. widgets.append(w);
    16. layout()->addWidget(w);
    17. showCurrentWidget();
    18. }
    19.  
    20. QWidget * StackedWidget::currentWidget()
    21. { return widgets.at(curr_index); }
    22.  
    23. void StackedWidget::setCurrentIndex(int i)
    24. {
    25. curr_index = i;
    26. showCurrentWidget();
    27. }
    28.  
    29. void StackedWidget::showCurrentWidget()
    30. {
    31. if (widgets.count() > 0)
    32. {
    33. foreach (QWidget * widget, widgets)
    34. widget->hide();
    35.  
    36. widgets.at(curr_index)->show();
    37. updateGeometry();
    38. }
    39. }
    40.  
    41. QSize StackedWidget::sizeHint()
    42. {
    43. if (auto_resize
    44. && count() > 0)
    45. return currentWidget()->minimumSize();
    46. else
    47. return QWidget::sizeHint();
    48. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can i achieve an auto-resizing QStackedWidget?

    Thanks truefusion. It works for me. However, I believe the code posted is your first draft E.g. I believe 'layout' in the constructor should be a member, not just local var.

    I have a question (apologize since I'm new to Qt), how and where should we release the memory in heap as it is claimed by QList<QWidget *> widgets; ?

Similar Threads

  1. auto resizing column/row widths
    By raman_31181 in forum Qt Programming
    Replies: 2
    Last Post: 11th September 2008, 19:16
  2. How to achieve showAll ?
    By Gopala Krishna in forum Qt Programming
    Replies: 5
    Last Post: 5th October 2007, 15:26
  3. hide() and auto resizing
    By MrGarbage in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2007, 10:44
  4. form not auto resizing
    By quickNitin in forum Newbie
    Replies: 5
    Last Post: 7th June 2007, 11:00
  5. How to achieve Uniformity...!!!
    By deepusrp in forum Qt Programming
    Replies: 9
    Last Post: 7th May 2007, 10:11

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.