How to set MainWindow at the center of any desktop ?
-
Hi guys, i tried to implement the QDesktopWidget but seems obsolete.
There is a method that can take the mainWindow and place it in the middle of the desktop regardless of the window size? If not how can i achieve it ?here my main.cpp
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.setMinimumSize(1360,710); w.setMaximumSize(1360,710); //w.centerInDesktop(?) something like this (?) w.show(); return a.exec(); } -
@JonB Hi, thank you for your answer. I Tried like this:
int x,y; x=w.screen()->availableSize().width()/7; y=w.screen()->availableSize().height()/6; w.move(x,y);It works , but it is not /2.
I mean, if i try it with the /2 it shows the window in between my two monitor, like, my bottom right of the first monitor and the bottom left of the second monitor.
(A little bit more in the first monitor though) anyway;
I then tried to "adjust" it changing the number until it shows me what i want(?), but it is not the right way to do it. isn't it?
I mean if i deploy my app and run it in a different monitor it would appear in the wrong position so i guess i shouldn't use the /7 & /6.@JonB "so that you can first subtract your window's size from" My MainWindow in the ui file is 1920x1080, but then i set it to 1360x710. There is a difference between my screen size and window size?
I tried like this:
QScreen screen; QSize newDimensions; newDimensions=w.screen()->size()-w.screen()->availableSize()/2; w.move(newDimensions);So "logically speaking" should it be:
new screen size ((1920x1080) - (1360x710))/2 ;
w.move(new screen size);
Is this correct?
@JonB Thanks for your patient.It works , but it is not /2.
I'm sorry, you'll have to do your own math, and think about it logically.
If, say, I have a screen 800 wide, and a window 200 wide, and I want it centred, I will go
800 - 200 == 600, 600 / 2 == 300, so I will place it at 300 from the left, it will end at 500, and sure enough that is also 300 from the right, so right in the middle. Draw on a piece of paper if it helps.I don't know about your multiple monitors, I don't use them! You will have to adjust for that.
-
You can get current screen from your window
w.screen(), then use availableSize to get screen dimensions, then you can callw.resize(newDimensions)to put the window in the centre.The user will still be able to move and resize the window, though.
-
You can get current screen from your window
w.screen(), then use availableSize to get screen dimensions, then you can callw.resize(newDimensions)to put the window in the centre.The user will still be able to move and resize the window, though.
@sierdzio "You can get current screen from your window w.screen(), then use availableSize to get screen dimensions, then you can call w.resize(newDimensions) to put the window in the centre".
Hi, thank you for the answer, so i tried like this but it doesn't work :QSize newDimensions; newDimensions=w.screen()->availableSize(); w.resize(newDimensions);But like this i'm just getting and then setting the same dimensions of the window.
It doesn't change the position of the window.@sierdzio "The user will still be able to move and resize the window, though."
How can the window be resized when i have set the same minimum & maximum size ?
And why i have to resize the mainWindow? i don't want to resize it, i have already set the size that i want with this:w.setMinimumSize(1360,710); w.setMaximumSize(1360,710);What am i doing wrong ?
@sierdzio Thanks for your patient.
-
@sierdzio "You can get current screen from your window w.screen(), then use availableSize to get screen dimensions, then you can call w.resize(newDimensions) to put the window in the centre".
Hi, thank you for the answer, so i tried like this but it doesn't work :QSize newDimensions; newDimensions=w.screen()->availableSize(); w.resize(newDimensions);But like this i'm just getting and then setting the same dimensions of the window.
It doesn't change the position of the window.@sierdzio "The user will still be able to move and resize the window, though."
How can the window be resized when i have set the same minimum & maximum size ?
And why i have to resize the mainWindow? i don't want to resize it, i have already set the size that i want with this:w.setMinimumSize(1360,710); w.setMaximumSize(1360,710);What am i doing wrong ?
@sierdzio Thanks for your patient.
@ApprenticeReno
To move where a window is you can usesetGeometry()or justmove(). What you need thescreen()for is to get the size so that you can first subtract your window's size from and then divide x & y by 2 to achieve the centering. -
@ApprenticeReno
To move where a window is you can usesetGeometry()or justmove(). What you need thescreen()for is to get the size so that you can first subtract your window's size from and then divide x & y by 2 to achieve the centering.@JonB Hi, thank you for your answer. I Tried like this:
int x,y; x=w.screen()->availableSize().width()/7; y=w.screen()->availableSize().height()/6; w.move(x,y);It works , but it is not /2.
I mean, if i try it with the /2 it shows the window in between my two monitor, like, my bottom right of the first monitor and the bottom left of the second monitor.
(A little bit more in the first monitor though) anyway;
I then tried to "adjust" it changing the number until it shows me what i want(?), but it is not the right way to do it. isn't it?
I mean if i deploy my app and run it in a different monitor it would appear in the wrong position so i guess i shouldn't use the /7 & /6.@JonB "so that you can first subtract your window's size from" My MainWindow in the ui file is 1920x1080, but then i set it to 1360x710. There is a difference between my screen size and window size?
I tried like this:
QScreen screen; QSize newDimensions; newDimensions=w.screen()->size()-w.screen()->availableSize()/2; w.move(newDimensions);So "logically speaking" should it be:
new screen size ((1920x1080) - (1360x710))/2 ;
w.move(new screen size);
Is this correct?
@JonB Thanks for your patient. -
@JonB Hi, thank you for your answer. I Tried like this:
int x,y; x=w.screen()->availableSize().width()/7; y=w.screen()->availableSize().height()/6; w.move(x,y);It works , but it is not /2.
I mean, if i try it with the /2 it shows the window in between my two monitor, like, my bottom right of the first monitor and the bottom left of the second monitor.
(A little bit more in the first monitor though) anyway;
I then tried to "adjust" it changing the number until it shows me what i want(?), but it is not the right way to do it. isn't it?
I mean if i deploy my app and run it in a different monitor it would appear in the wrong position so i guess i shouldn't use the /7 & /6.@JonB "so that you can first subtract your window's size from" My MainWindow in the ui file is 1920x1080, but then i set it to 1360x710. There is a difference between my screen size and window size?
I tried like this:
QScreen screen; QSize newDimensions; newDimensions=w.screen()->size()-w.screen()->availableSize()/2; w.move(newDimensions);So "logically speaking" should it be:
new screen size ((1920x1080) - (1360x710))/2 ;
w.move(new screen size);
Is this correct?
@JonB Thanks for your patient.It works , but it is not /2.
I'm sorry, you'll have to do your own math, and think about it logically.
If, say, I have a screen 800 wide, and a window 200 wide, and I want it centred, I will go
800 - 200 == 600, 600 / 2 == 300, so I will place it at 300 from the left, it will end at 500, and sure enough that is also 300 from the right, so right in the middle. Draw on a piece of paper if it helps.I don't know about your multiple monitors, I don't use them! You will have to adjust for that.
-
@JonB Hi, thank you for your answer. I Tried like this:
int x,y; x=w.screen()->availableSize().width()/7; y=w.screen()->availableSize().height()/6; w.move(x,y);It works , but it is not /2.
I mean, if i try it with the /2 it shows the window in between my two monitor, like, my bottom right of the first monitor and the bottom left of the second monitor.
(A little bit more in the first monitor though) anyway;
I then tried to "adjust" it changing the number until it shows me what i want(?), but it is not the right way to do it. isn't it?
I mean if i deploy my app and run it in a different monitor it would appear in the wrong position so i guess i shouldn't use the /7 & /6.@JonB "so that you can first subtract your window's size from" My MainWindow in the ui file is 1920x1080, but then i set it to 1360x710. There is a difference between my screen size and window size?
I tried like this:
QScreen screen; QSize newDimensions; newDimensions=w.screen()->size()-w.screen()->availableSize()/2; w.move(newDimensions);So "logically speaking" should it be:
new screen size ((1920x1080) - (1360x710))/2 ;
w.move(new screen size);
Is this correct?
@JonB Thanks for your patient.QPoint center=w.windowHandle()->screen()->availableGeometry().center(); w.move(center-QPoint(w.size().width()/2,w.size().height()/2)); -
It works , but it is not /2.
I'm sorry, you'll have to do your own math, and think about it logically.
If, say, I have a screen 800 wide, and a window 200 wide, and I want it centred, I will go
800 - 200 == 600, 600 / 2 == 300, so I will place it at 300 from the left, it will end at 500, and sure enough that is also 300 from the right, so right in the middle. Draw on a piece of paper if it helps.I don't know about your multiple monitors, I don't use them! You will have to adjust for that.
@JonB Now i finally got it, thank you very much.
-
QPoint center=w.windowHandle()->screen()->availableGeometry().center(); w.move(center-QPoint(w.size().width()/2,w.size().height()/2));