调整窗口大小以适合内容 [英] Resize window to fit content

查看:149
本文介绍了调整窗口大小以适合内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QGLWidget,我想将其调整为给定的分辨率(大于包含的窗口). 我的意图是,窗口会一直扩展到小部件放进内部,但找不到找到它的方法.

I have a QGLWidget, which I want to resize to a given resolution (bigger than the containing window). My intention is, that the window expands until the widget fits inside, but can't find a way to do it.

在调整QGLWidget的大小后,我尝试了一些命令,使其无法成功运行. 我将在这里列出结果:

I tried several commands after resizing the QGLWidget to make it work without success. I will list the results here:

  • 不执行其他任何操作:窗口小部件会覆盖整个窗口.最终将调整其大小以适合较小的窗口
  • mainWindow.adjustSize():窗口小部件的大小调整为(0,0)
  • mainWindow.resize(mainWindow.sizeHint()):请参见上文
  • mainWindow.resize(mainWindow.minimumSizeHint()):见上文
  • do nothing else: The Widget overlaps the whole window. Eventually it will be resized to fit back into the smaller window
  • mainWindow.adjustSize(): The widget gets resized to (0, 0)
  • mainWindow.resize(mainWindow.sizeHint()): see above
  • mainWindow.resize(mainWindow.minimumSizeHint()): see above

我还阅读了此线程,在执行mainWindow调整大小之前,我会需要运行循环来重新计算新的尺寸,因此我插入了QCoreApplication::processEvents来这样做,而没有任何可见的效果.

I also read in this thread, that before doing the mainWindow resize I the event loop needs to be run to recalculate the new sizes, so I inserted QCoreApplication::processEvents to do so, without any visible effect.

那我该如何通过小部件调整窗口的大小?

So how do I resize the window via the widget?

修改

GLWidget不是窗口的唯一窗口小部件. 它与分组框一起嵌入在拆分器中.

The GLWidget is not the only widget of the window. It is embedded in splitter together with a group box.

推荐答案

http://qt-project.org/doc/qt-4.8/qsizepolicy.html#Policy-enum

http://qt-project.org/doc/qt- 4.8/qwidget.html#setFixedSize

因此,假设您将QGLWidget嵌套在QMainWindow中作为中央窗口小部件,则需要设置QGLWidget的尺寸策略.

So assuming that you have your QGLWidget nested inside your QMainWindow as the central widget, you need to set the size policy of your QGLWidget.

例如:

QGLWidget * glw; // in your header for QMainWindow

...

// In your constructor for QMainWindow
glw = new QGLWidget;
this->setCentralWidget(glw);
glw->setFixedSize(500, 500);

this->adjustSize();

希望有帮助.

这篇关于调整窗口大小以适合内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆