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.
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.
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.
Right click on your main form and select Size Constraints -> Set Minimum Size and then Size Constraints -> Set Maximum Size.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With