PDA

View Full Version : making MainWindow size to possible minimum size



Alex22
24th December 2015, 20:00
hi
i designed all of my widgets in the MainWindow by using QGridlayout. as you know in this way some widgets are bigger than normal size and each widget takes more space that while manually i want to make it to be small, i must pull MainWindow by mouse manually. please help me to make its size how much small that how much is possible like this: myMainWindow.size(min, min).

ChrisW67
24th December 2015, 23:20
What happens if you call resize(10,10) after the window is displayed?

Alex22
25th December 2015, 05:27
Thanks ChrisW67, How can I place it in central. now when I want to show MainWindow, it shows in a small size ( that i wanted) but it goes to a bad position and I must drop it by mouse clicking in central.
thanks for any help

alainstgt
25th December 2015, 10:50
use setGeometry( int xPosition, int yPosition, int width, int height ), you get all you want here, no more need of resize()

Alex22
25th December 2015, 11:36
use setGeometry( int xPosition, int yPosition, int width, int height ), you get all you want here, no more need of resize()

thanks, what are x and y positions numbers for the central position of my monitor? i want it to be showed in central position while showing.

ChrisW67
26th December 2015, 01:21
No, x and y are the top left corner. You will have to calculate where to position it based on the geometry of the desktop and the size of the main window.

Widget geometry property (http://doc.qt.io/qt-5/qwidget.html#geometry-prop)
QDesktopWidget::screenGeometry()

alainstgt
26th December 2015, 17:33
The following code snippet is doing what you are looking for:

// set width and height of your window
int width = ...
int height = ...
// alternatively you can get your window size
// if already set:
/* adjustSize(); // necessary to get updated size
QRect frame = geometry();
int width = frame.width();
int height = frame.height(); */

// get screen geometry
QRect screen = QApplication::desktop()->screenGeometry();

// we center window on screen
int x = ( screen.width - width ) >> 1;
int y = ( screen.height - height ) >> 1;
setGeometry( x, y, width, height );