⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Problems centering a QLineEdit into a VLayout

  • Hi,

    I have a QWizardPAge where I have a Vertical layout (Vlay = new QVBoxLayout();) and inside it i have a label (QLabel *my_label) and then a QLineEdit (QLineEdit *le_Per;)
    I add the Vertical layout to the general one like this:


    Until here its all okey, I see the label and down of it I see the LineEdit BUT then I want the LineEdit centered on the screen, so I tried with:


    But its still on the left side of the screen. I just want the lineEdit on the center, the label must be on the left side to be easy to read as it is 2 lines long of information.

    I've trie d too with:

    le_Per->setStyleSheet("QLineEdit{margin-left: 100px;}");

    But instead of moving the all widget, it moves the start of it and not the end so the widget becomes shorter than it was so... if I try to center it with a 300px the widget (LineEdit) dissapears .

    Thank you so much!

  • Moderators

    If I understand the question correctly, hopefully I do, you could insert a spacer to left and right of the line edit. That should do the trick. Here is a quick reference: http://doc.qt.io/qt-5/qspaceritem.html

    Kind regards.

  • @kshegunov I've see some examples but if I am right, I will need a HLayout also wich I'm trying to avoid (to minimize the quantity of widgets)

  • Moderators

    Yes, you will probably do. Widgets are pretty thin (resources-wise) so in most cases creating lots of them is not a problem. For your problem you might also consider a form or a grid layout, they might be a more suitable choice.

  • @kshegunov I've had to say that I was trying to avoid using more widgets like the HLayout . I'll consider your option about the form and the grid if I can't do it withouth adding more widgets.

    Thank you so much!

  • Moderators

    You are welcome. If you are satisfied with the answer, please mark the question as solved.
    Good luck.

  • @kshegunov yes yes I know how that page works, but thanks for remind me it. It doesn't really satisfy me because it's not how I want it to work but yes, it does answer my question correctly :)

  • Moderators

    I don't change the page, but there is a button at the bottom Thread tools or something like this, when you click it there should be a Mark as solved option. ;)
    Kind regards.

Log in to reply