Search code examples
qtpyqtqstackedwidget

How to make different pages of a QStackedWidget of different sizes?


I have a QStackWidget with 2 pages. One page contains just 2 QLineEdit widget and another page contains a large QTextEdit. Does anyone know how to make one page larger than another so that the window still fits tightly for each page? In the Qt Designer, the QStackWidget has to take the larger size in order to first the QTextEdit, but then the page with the QLineEdit widgets would be way too big.

Thanks


Solution

  • You can set the size policy of the page that is displayed to QSizePolicy.Preferred and the other ones to QSizePolicy.Ignored. After that call adjustSize to update the sizes. For example when you set the current index to show page1 :

    page1.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
    page2.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
    
    stackedWidget.adjustSize()
    adjustSize()
    

    And when the other page is shown, set the size policies the other way.