Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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.
    0_1551804394364_ec68e764-027d-4101-8d76-11b3377f65eb-image.png

    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.
    0_1551804530215_bb01fbb4-a561-4de3-967f-94c873a5b9af-image.png

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

    weightEntry = new QStackedWidget; 
    weightKgTxt = new QLineEdit();
    weightLbTxt = new QLineEdit();
    weightEntry->addWidget(weightKgTxt);
    weightEntry->addWidget(weightLbTxt)
    

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

    weightEntry->setSizePolicy(QSizePolicy::Minimum);
    
    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

    Hi,

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

    `weightEntry.setMaximumHeight(weightKgTxt->sizeHint().height());


Log in to reply