Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: Set size of QMainWindow

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.

like image 389
dadod2 Avatar asked Apr 29 '13 13:04

dadod2


People also ask

How do I set the size of a QMainWindow?

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.

What is QMainWindow in Qt?

Qt Main Window Framework A main window provides a framework for building an application's user interface. Qt has QMainWindow and its related classes for main window management. QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar.

How do I fix the window size in Qt?

Right click on your main form and select Size Constraints -> Set Minimum Size and then Size Constraints -> Set Maximum Size.

What is Qt window?

Qt provides the following classes for managing main windows and associated user interface components: QMainWindow is the central class around which applications can be built. Along with the companion QDockWidget and QToolBar classes, it represents the top-level user interface of the application.


2 Answers

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.

like image 188
muesli Avatar answered Sep 18 '22 14:09

muesli


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); 
like image 26
3 revs, 2 users 81% Avatar answered Sep 18 '22 14:09

3 revs, 2 users 81%