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. BR, Pico Here is my code segment**************************************************************************************************
- mButtonLayout = new ButtonLayout(centralwdg); // this layout is left pane
- mRawDataView = new RawDataView(mSplitterRight);
- mSplitterRight->addWidget(mRawDataView);
- mSplitterRight->addWidget(mTableView);
- mSplitterRight->addWidget(mDumyWidget);
- mSplitterRight->setCollapsible(0, false);
- mSplitterRight->setCollapsible(1, false);
- mSplitterRight->setCollapsible(2, false);
- mDumyWidget->hide();
- //mainLayout->addWidget(mSplitter);
- mainLayout->addWidget(mButtonLayout);
- mainLayout->addWidget(mSplitterRight,1);
- setCentralWidget(centralwdg);
7 replies
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.
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.
BR,
Pico
pico: We have excellent documentation [doc.qt.nokia.com] 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 [qt.nokia.com]. All I have read do cover the layout system extensively.
Best Regards,
Tobias
You must log in to post a reply. Not a member yet? Register here!





