How to position windows in the bottom right of the screen?
-
Hi,
I've looked all over the place and haven't found an answer (sorry if there is one) but I've been messing around with QT and trying different things, but one thing I wanted to do recently was have my mainwindow and any other form open in the bottom right corner of the screen, as it's a small window that just needs to show up out of the way (in the bottom right corner), while looking online I found the following code which works for setting the screen to the top left, but when I modified the code for bottom right, the form just disappears, it shows up in the taskbar, but it's as if it moves to the bottom right from the top corner and pushes it off the screen.
Can anyone help me please?
#include "mainwindow.h" #include <QApplication> #include <QScreen> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QRect screenrect = a.primaryScreen()->geometry(); w.move(screenrect.top(), screenrect.left()); //Note: I changed the above to be bottom and right. But I thought I would show the original code. w.show(); return a.exec(); }
Thanks,
Dan -
You should be aware that w.move() takes the topLeft position of your widget so moving it to .bottom() will move it out of the window.. Same goes for .right()
-
@Christian-Ehrlicher - Ah OK, so how can I fix this? As I mentioned, I'm new to this, so what should I change it to?
-
Don't know what's Qt specific here but when you have the lower right corner of a rectangle and need the upper left you must subtract the width and height of it.
-
@dannymozzer said in How to position windows in the bottom right of the screen?:
QRect screenrect = a.primaryScreen()->geometry();
w.move(screenrect.top(), screenrect.left());
Try setting like this for positioning bottom right corner
w.move(screenrect.right()-w.width(), screenrect.bottom()-w.height()); -
@nagesh - Hmm... that seemed to do the trick, but the bottom of the window gets cut off, no matter how large the window is, it appears in the bottom right for sure, but as I said, the bottom of the window seems to appear behind the taskbar.
Thanks -
Hi,
So it's in the right place. The screen size is exactly the screen size and does not care for the task bar.
-
Use QScreen::availableGeometry. It provides the geometry excluding window manager reserved areas.