PDA

View Full Version : Centre the form when maximised



premroxx
12th September 2011, 06:25
Hi,
I have a form that is roughly half the width of the screen. By default, When the exe screen is maximised the form gets aligned to the left side of the screen. What i want to do is to make it appear in the centre 6837 How can i do that? Thanks

ChrisW67
12th September 2011, 08:12
Use a layout, because at the moment it doesn't look like your main window has a layout applied.

premroxx
14th September 2011, 03:22
What you see is just a begin screen. I have lots of labels, texboxes & comboxes etc in the form. To add a layout at this point might a bit tedious. Is there anyway i can change it through coding.

ChrisW67
14th September 2011, 04:16
Sure, you can capture the QWidget::resizeEvent() of the main window, manually recalculate the size and location of each widget, and manually set their geometry. This is generally considered tedious, which is why layout engines exist.

How have you built the form? Designer or in code?

premroxx
14th September 2011, 05:08
I have built the form using designer. I have a series of labels & textboxes & images on top of each other. I display them based on conditions. Because the images and the widgets are on top of each other its kinda difficult to use layout as it might mess up the whole thing or is there an better/correct way of using layout?

ChrisW67
14th September 2011, 06:07
You should look at QStackedWidget as a way to handle several layers of widgets that you can switch between.

wysota
14th September 2011, 09:33
Have a look at this article: The Panel Stack Pattern

kalgorithmist
14th September 2011, 18:37
This may look crazy, but it works. Consider this code in the constructor of the form:


int newX = QApplication::desktop ()->width () / 2;

newX -= this->width () / 2;

this->setGeometry (newX, 0, this->width (), this->height ());


In Line 1: QApplication::desktop ()->width () gets the maximum width of the current monitor where the app is

Do not forget to #include <QDesktopWidget> and #include <QApplication> .

ChrisW67
14th September 2011, 23:49
Sure, this will place "this" in the middle of the desktop.

That, however, is not what I understand premroxx was asking for. Premroxx has maximised "this" and the numerous, absolutely placed contents of "this" are not being placed as expected. The problem would also manifest if premroxx resized the window without maximising it. Premroxx has also tried to mimic a variable interface by overlaying widgets and manually controlling their individual visibility; a very VB6 way of going about the whole exercise.

kalgorithmist
15th September 2011, 00:14
Silly me :o . In this case, I think he wants what a QGridLayout does.

premroxx
18th September 2011, 06:25
I tried the form layout. In the 'Object inspector' i right click the 'MainWindow' then layout->layout in a form layout. I dragged the red margin, so that it covers the form. Then in the layoutformalignment i changed to alignHcentre,AlignVcentre. Still it seems to aligned towards left side. What am i doing wrong here?

ChrisW67
19th September 2011, 01:18
QFormLayout is designed to arrange labels and associated data entry widgets in two columns with convenience features related to setting widget buddies. You probably have you content widget in the left column. I would suggest that you look at a QHBoxLayout with the various alignment values.