How can I get a QStackedWidget to automatically switch size depending on the content of the page?

When having a QStackedWidget [doc.qt.nokia.com] that contains pages that you want to display in different sizes, then it can adjust its size automatically when switching pages if you follow these steps:

  • set the size policy of the invisible pages to ignored
  • set the size policy of the page that is going to be visible to e.g preferred
  • call adjustSize() [doc.qt.nokia.com] in order to adjust the size to the sizeHint() [doc.qt.nokia.com].

This article() [doc.qt.nokia.com] in Qt Quarterly demonstrates how this can be done. It is written for Qt 3, but the concept remains the same for Qt 4:

The example below illustrates how this can be done:

  1. #include <QtGui>
  2. class SubWidget : public QWidget
  3. {
  4.     Q_OBJECT
  5. public:
  6.     SubWidget(QWidget *parent) : QWidget(parent)
  7.     {
  8.         table = new QTableWidget(this);
  9.         table->setColumnCount(10);
  10.         table->setRowCount(10);
  11.         QVBoxLayout *layout = new QVBoxLayout(this);
  12.         layout->addWidget(table);
  13.  
  14.     }
  15.     QSize minimumSizeHint () const
  16.     {
  17.         int newWidth = 0;
  18.         int newHeight =0;
  19.         for (int i = 0; i < table->columnCount(); i++) {
  20.             newWidth+= table->columnWidth(i);
  21.         }
  22.         for (int y = 0; y < table->rowCount(); y++) {
  23.             newHeight+= table->rowHeight(1);
  24.         }
  25.         newWidth+= table->verticalHeader()->width() + 2 *table->frameWidth();
  26.         newHeight+= table->horizontalHeader()->height() +2 *table->frameWidth();
  27.         return QSize(newWidth, newHeight);
  28.     }
  29. private:
  30.     QTableWidget *table;
  31. };
  32.  
  33. class Widget : public QWidget
  34. {
  35.     Q_OBJECT
  36. public:
  37.     Widget() : QWidget()
  38.     {
  39.         QPushButton *firstButton = new QPushButton("Open second page", this);
  40.         QPushButton *secondButton = new QPushButton("Open first page", this);
  41.         stackWidget = new QStackedWidget(this);
  42.  
  43.         SubWidget *firstPage = new SubWidget(stackWidget);
  44.  
  45.         QWidget *secondPage = new QWidget(stackWidget);
  46.         QLineEdit *edit2 = new QLineEdit(secondPage);
  47.         QVBoxLayout *secondPageLayout = new QVBoxLayout(secondPage);
  48.         secondPageLayout->addWidget(edit2);
  49.  
  50.         stackWidget->addWidget(firstPage);
  51.         stackWidget->addWidget(secondPage);
  52.  
  53.         QVBoxLayout *layout = new QVBoxLayout(this);
  54.         layout->addWidget(secondButton);
  55.         layout->addWidget(firstButton);
  56.         layout->addWidget(stackWidget);
  57.  
  58.         connect(firstButton, SIGNAL(clicked()), this, SLOT(test1()));
  59.         connect(secondButton, SIGNAL(clicked()), this, SLOT(test2()));
  60.  
  61.     }
  62.     public slots:
  63.         void test1()
  64.         {
  65.             changeCurrent(1);
  66.         }
  67.  
  68.         void test2()
  69.         {
  70.             changeCurrent(0);
  71.         }
  72. private:
  73.     void changeCurrent(int idx)
  74.     {
  75.         if (stackWidget->currentWidget() !=0) {
  76.             stackWidget->currentWidget()->setSizePolicy(QSizePolicy::Ignored,
  77.                                                         QSizePolicy::Ignored);
  78.         }
  79.         stackWidget->setCurrentIndex(idx);
  80.         stackWidget->currentWidget()->setSizePolicy(QSizePolicy::Expanding,
  81.                                                     QSizePolicy::Expanding);
  82.         adjustSize();
  83.     }
  84.     QStackedWidget *stackWidget;
  85. };
  86.  
  87. #include "main.moc"
  88.  
  89. int main(int argc, char** argv)
  90. {
  91.     QApplication app(argc, argv);
  92.     Widget window;
  93.     window.show();
  94.     return app.exec();
  95.  
  96. }

No comments

Write a comment

Sorry, you must be logged in to post a comment.