Reputation: 545
I'm new to Qt, so I wonder whether there is a way to set the size of a QMainWindow
to (for example) 70% of the user's desktop.
I tried the stretch factor but it didn't work. QWidget::setFixedSize
worked but only with a pixel number, I think.
Upvotes: 39
Views: 99696
Reputation: 9525
Just to update @muesli's answer for Qt6, QDesktopWidget
had been deprecated in Qt5 and in Qt6 is removed.
The new equivalent code is
resize(QGuiApplication::primaryScreen()->availableGeometry().size() * 0.7);
Upvotes: 2
Reputation: 2158
Thanks to Amir eas. The problem is solved. Here's the code for it:
#include <QDesktopWidget>
#include <QMainWindow>
...
QDesktopWidget dw;
MainWindow w;
...
int x=dw.width()*0.7;
int y=dw.height()*0.7;
w.setFixedSize(x,y);
Upvotes: 36
Reputation: 51
You can use the availableGeometry(QWidget*)
method in QDesktopWidget
, this will give you the geometry of the screen that this widget is currently on.
For example:
QRect screenSize = desktop.availableGeometry(this);
this->setFixedSize(QSize(screenSize.width * 0.7f, screenSize.height * 0.7f));
Where this
is the MainWindow pointer.
This will work when using multiple screens.
Upvotes: 5
Reputation: 815
Somewhere in your QMainWindow constructor, do this:
resize(QDesktopWidget().availableGeometry(this).size() * 0.7);
This will resize the window to 70% of the available screen space.
Upvotes: 52