Important: Please read the Qt Code of Conduct -

How could I fix (x, y) of a widget in a mainwindow?

  • Hi, Sir:
    I have a widget and a splitter in a mainwindow, and, I insert them into a QHBoxLayout by using addwidget().
    And, the widget has several buttons.

    After running, things look good. But, if I expand the window's size, the first button's position will go down.

    Could anyone teach me how to fix the position of these buttons even I make the window's size bigger?

    Thanks a lot.



    Here is my code segment

    QWidget * centralwdg = new QWidget(this); //centralwdg is central widget

    QHBoxLayout * mainLayout = new QHBoxLayout(centralwdg);
    mButtonLayout = new ButtonLayout(centralwdg); // this layout is left pane
    mSplitterRight = new QSplitter(Qt::Vertical, centralwdg);//this splitter would be for other views
    mRawDataView = new RawDataView(mSplitterRight);
    mDumyWidget = new QWidget(mSplitterRight);
    mTableView = new QTableView(mSplitterRight);
    mSplitterRight->setCollapsible(0, false);
    mSplitterRight->setCollapsible(1, false);
    mSplitterRight->setCollapsible(2, false);


  • Moderators

    If you leave out the layouts then you can position widgets as x, y positions, but that is most likely not what you want: Resizing will no longer work properly then as the widgets will stay at their given position no matter how big the window is.

    What I think you want to do is either fix the vertical size of the widgets or add some spacer element above or below your horizontal layout (place the horizontal layout inside a vertical one and add a spacer element where desired). That way the spacer will expand when resizing the window vertically and not the elements inside your horizontal layout.

    I recommend playing with the UI designer: It is really easy to change add/remove layouts there, it offers immediate feedback on the effects of the changed settings and is way simpler to edit than hardcoded UIs, too.

    Even when insisting on hardcoding UIs the designer is a great tool to experiment with layouts in Qt.

  • I totaly agree with Tabias. The easiest way to create and test the form, is to use the designer. You can create a compex layout without coding one line. And best you can see what happens in the preview. This saves a lot of time for me!

  • [quote author="Tobias Hunger" date="1294481222"]Even when insisting on hardcoding UIs the designer is a great tool to experiment with layouts in Qt.[/quote]

    And it's easy to call uic manually on the .ui file, grab the generated code for the UI setup and adapt it further. That helped me a lot in the past.

  • @Volker, that is a really good tip, thank you

    //i used to look at ui_CLASS.h implementation (and sometimes use layouts names to add "coded" widgets to them) but i didn't had the idea of getting the code from there, thanks ;)

  • Hi, Tobias:
    Thanks for your recommendations.

    I indeed use Qt Designer sometimes, but, I don't know how to integrate the different components (not basic components) in a layout now. So, I don't use it here.

    I want to add two different components in a window. But, I just want to fix (x, y) of the top-left position of the first components (It indeed is a vertical layout with several buttons and a vertical spacer). So, if I can not use another vertical layout to include original horizontal layout and new spacer. It seems to limit the second component's height.

    Could you teach me more to solve this view issue?

    Thanks a lot.

  • Moderators

    pico: We have "excellent documentation": which introduces the layout system. Please read it.

    If our documentation does not help then I can point you to lots of "great books on Qt programming": All I have read do cover the layout system extensively.

    Best Regards,

  • Hi, Tobias:
    Thanks a lot.
    I will read them firstly.

Log in to reply