Qt: Set size of QMainWindow

痞子三分冷 提交于 2020-01-01 07:46:06

问题


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.


回答1:


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);



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/16280323/qt-set-size-of-qmainwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!