Important: Please read the Qt Code of Conduct -

Settings margins within a QStackedWidget

  • I am coding an application that includes a dialog for entering a person's information. These entries are mostly text entries, so I am using QLineEdit() s.

    I would like to put the QLineEdits in a QStackedWidget so that alternative input methods can be used (for instance, entering Height in cm requires 1 text box, but entering Height as Ft and Inches requires 2). When I add the QLineEdit to a QStackedWidget and add the QStackedWidget to the form, the QLineEdits are expanded to fill the space inside the layout.

    Here is the dialog with no QStackedWidgets, just the QLineEdit objects added to the main layout. As you can see everything is spaced properly and evenly.

    Here is the dialog with the Height and Weight QLineEdit objects inside QStackedWidgets. The Height and Weight QLineEdit objects are expanded to fill the layout.

    Here is my code for creating the Weight QStackedWidget, for example:

    weightEntry = new QStackedWidget; 
    weightKgTxt = new QLineEdit();
    weightLbTxt = new QLineEdit();

    I have tried using the setSizePolicy() method of QStackedWidget but I get an error :

    error: calling private constructor of class QSizePolicy

    How can I change the layout or format of QStackedWidget so the QLineEdits inside will be evenly spaced from everything else, like my first picture?

  • Lifetime Qt Champion


    One thing you can do since you only have line edits in your stacked widget is to use something like:


Log in to reply