How to pin widget to left/right/top/bottom edge?

  • Hi everyone,

    Today question from UI's modeling.

    I'd like to dynamicly display different QWidget Form in one area of Form. Simple preview:

    Main area to load my widgets:

    ! area for loading widgets)!

    And one of my widgets which will be loaded to that area:

    ! widget to load)!

    The first thing is, how to force aligning my loaded widget forms to this specified area?
    And the second thing is, how to align that widget's area to sides of window? For example, I'd like to pin area to left, top and right edges of window.

    Another thing is, how to pin each component (object) of my widget's Form to specified side? For example, I'd like to pin button "Add" to left bottom corner of widget's form and other two buttons pin to right bottom corner. And their should stay on its places when user resize window.

    As I Googled, this could be done by Layouts in Qt Designer, but everytime I set some layout then buttons are resized, even if I use Horizontal Spacers.

    This is very cute solved in Visual Studio, because the only I have to do is to set aligment for each object on Form. Is something similar in Qt Designer?

  • Set the buttons to fixed size, add spacers and all done. There are some good QtDesigner tutorials around. Try then ;-)

  • I tried everything, all those settings of sizes or position strategy and it doesn't work for me... All objects stay static irrespective of window resizing or objects are resizing as well ;/

  • Moderators

    Am I right you are comming from a .Net'ish world? VS and pinning to sides sounds like it :)

    What you describe is closer to what QML in Qt Quick uses.
    With Qt widgets it works a little different. You set layouts to widgets and they take care of proper aligning and resizing. There is no pinning to sides etc.

    A documentation for it is "here":

    As a short example:
    ! example)!

    The blue bars icons indicate that a layout is set, either horizontal or vertical. It basically indicates if items are stacked top to bottom or side by side. There is also a grid layout and form layout (not used here). To "push" items you use spacers (I call them springs :) ).

  • bq. All objects stay static

    What a lot of Qt starters forget is to set the layout of the top widget. Select the widget you placed and click the e.g. horizontal layout. This will set the Widget layout just as Chris indicated above with a QMainWindow which is a fancy form of a Widget. The widget will now do resizing etc and all its childern will comply to that action.

  • Ok... Seems it's working...

    Now little harder thing...
    I have few "resizable" widget Forms like this:

    ! form)!

    and main window with "resizable" area where I want to load my widget Forms:

    ! anything here)!

    I'm loading Forms by:

    contacts = new ListOfContacts(ui->widget);

    Question is, how to load this form into widget (or replace with widget) to keep resizability? Size of loaded form should be adjusted to widget's size and resize when user resize main window.

  • Moderators

    You don't "load into a widget". As I said - you use layouts for positioning. Also show() is redundant. The widget will be shown automatically.
    So, if ui->widget is your "container", it goes something like this:
    //you don't need a parent here (but it's ok if it's there too)
    //it will be automatically reparented when inserted into a layout anyway
    contacts = new ListOfContacts();

    //create a layout, VBox, HBox, Grid or whatever
    //also don't need a parent. it will be reparented when assigned to widget
    QVBoxLayout* layout = new QVBoxLayout();

    //insert your new widget into that layout

    //assign this layout to your "container"

  • I don't know what's wrong, but it doesn't work for me...

    Even I tried replace widget for QGridLayout and just add my widget to GridLayout, but it's not what I want...

    This is my main window:

    ! layout)!

    And this is my widget:


    And my best result is (best means, loaded widget is resizable with window):

    ! window with loaded contact list)!

    And this is called by:
    contact_list = new ListOfContacts(ui->gridLayoutWidget);

    But it is not in it's place. Your code above gives similar effect.

  • Moderators

    You're doing some odd things in your main window. Why are you placing a spacer in a splitter? It makes no sense.

    Could you say what is it that you want exactly? I don't really get it.

    If you just want your contacts widget to fill main window then forget all those layouts and just use mainWindow->setCentralWidget(contacts).

    If you really want a resizable area via splitters then you need to put some widgets(or layouts) in that splitter. If there's only a spacer there Qt will assume it's an empty area(without widgets) and show no splitter handle.

    So with this kind of layout:
    ! layout)!

    You can add your widget to any of those areas, eg.
    which will give you this:
    ! window)!

    Windows 8 uses a weird color for splitter handles (transparent :) ) so I used this little stylesheet to show where they really are:
    setStyleSheet("QSplitter::handle { background-color: red; }");

  • Which version of Qt Creator u used? It doesn't work in my 2.6.2...

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.