Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

qt - Resize window to fit content

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.

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

  • 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

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?

Edit

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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

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.

For example:

QGLWidget * glw; // in your header for QMainWindow

...

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

this->adjustSize();

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...