36

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.

Aleksej
  • 22,443
  • 5
  • 33
  • 38
dadod2
  • 515
  • 1
  • 6
  • 11
  • 1
    See this link: And the you can calculate 70% of that. – Amir eas Apr 29 '13 at 14:07
  • amir go ahead and post as an answer. – UmNyobe Apr 29 '13 at 14:10
  • kay it worked i edited my question with my code ;) – dadod2 Apr 29 '13 at 14:29
  • @dado2: Could you write an answer to your own question instead of modifying your question ? This would respect the Q/A style that StackOverflow encourage. – ereOn Apr 30 '13 at 10:29
  • I have edited the question to improve grammar and formatting. Also I have added the solution as an answer to this question. If there's any problem with this - please comment - so that I can remove my answer and let @dadod2 or Amir eas write his answer here. – zeFree Apr 30 '13 at 10:39

4 Answers4

51

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.

muesli
  • 795
  • 6
  • 7
  • This should be the right answer, thanks! Still I wonder why this needs to be so cryptic in QT instead of simply being able to do something like `resize(640, 480)`. – Timo Ernst Mar 12 '20 at 14:19
  • 1
    @Timo: You can also do that, of course. But the idea is to adjust the window's size automatically in relation to the available screen resolution. – muesli Mar 13 '20 at 15:21
36

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);
E_net4
  • 27,810
  • 13
  • 101
  • 139
zeFree
  • 2,129
  • 2
  • 31
  • 39
  • 5
    This will not work so good for multi monitor setup :\ Any ideas for that? – chwi Mar 12 '14 at 11:48
  • 4
    @Wilhelmsen : you can use `QDesktiopWidget::primaryScreen()` like in the answer to this question: http://stackoverflow.com/questions/17893328/qt-getting-the-screen-resolution-without-the-extended-monitor – vsz Jan 14 '15 at 08:38
  • Beware that as a side effect, the user won't be able to resize the window at all with this method. See the answer below for a better approach. – Leogout Aug 19 '20 at 12:13
5

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.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
user3413918
  • 51
  • 1
  • 2
1

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);
jwezorek
  • 8,592
  • 1
  • 29
  • 46