MOC89 MOC89 - 2 months ago 8
C++ Question

Show Mainwindow in the right of the screen (Qt 5.1.0)

I want to show my mainwindow in the right side of the screen.

I use this code :

QRect r = this->frameGeometry();
r.moveRight(QDesktopWidget::availableGeometry());
this->move(r.topRight());


And I receive this error :


error: cannot call member function 'const QRect QDesktopWidget::availableGeometry(int) const' without object


If I use 1024 instead
QDesktopWidget::availableGeometry()
it works... But I don't want to initialize it staticly...

How can I dynamically reposition the window for different screen sizes?

Answer

QDesktopWidget::availableGeometry is not a static function. You can use QApplication::desktop() function to get a QDesktopWidget object:

QRect r  = this->frameGeometry();
r.moveRight(QApplication::desktop()->availableGeometry()); 

You will have to put something else in the moveRight() function. You can't put a QRect there. Maybe what you want to do is:

QRect r = QApplication::desktop()->availableGeometry();
r.setLeft(r.center().x());
this->resize(r.width(), r.height());
this->move(r.topLeft());

Or if you don't want to resize your window:

QRect r = QApplication::desktop()->availableGeometry();
QRect main_rect = this->geometry();
main_rect.moveTopRight(r.topRight());
this->move(main_rect.topLeft());
Comments