Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize window to fit content

Tags:

qt

qlayout

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.

like image 785
Nobody moving away from SE Avatar asked May 09 '13 18:05

Nobody moving away from SE


People also ask

How do I resize a window to fit the screen?

Move or resize a window using only the keyboard. Press Alt + F7 to move a window or Alt + F8 to resize. Use the arrow keys to move or resize, then press Enter to finish, or press Esc to return to the original position and size. Maximize a window by dragging it to the top of the screen.

How do you resize a window?

Right-click on the button that represents the window in Window List . Choose Resize from the Window Menu. Use the arrow keys to resize the window. Press-and-hold Alt, then middle-click near the corner that you want to resize.

How do I resize a window that is off screen Windows 10?

Move the mouse pointer to the middle of the screen. Use the ARROW keys on the keyboard to move the program window to a viewable area on the screen. Press ENTER. Resize the window if necessary.

How do I resize my browser window to a specific size?

Press Alt+Space to bring up the window menu, press S to choose the Size option, use the arrow keys to resize the window, and lastly Enter to confirm. Click the Maximize button in the top right corner of the window. Click the title bar and drag the window to the left, top, or right edge of the desktop.


2 Answers

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.

like image 166
phyatt Avatar answered Sep 20 '22 02:09

phyatt


I have an app that needed to be very similar to your requirements, so I'll post my solution here. An image covering the window which is freely expandable and shrinkable, and can be changed to the original size, and remain expandable / shrinkable after that.

I used a QLabel widget to display the image, but it should work with other widget types too. I created the widget with an initial size and the QSizePolicy::Ignored.

label->resize (w, h); // initial size
label->setSizePolicy (QSizePolicy::Ignored, QSizePolicy::Ignored);

The label widget was in a QVBoxLayout with a few buttons in the window, but this may work with other layout types too.

The window and image widget can be resized to the image's original size with this code:

label->resize (w, h); // change to original size
label->setMinimumSize (w, h); // prevent it from collapsing to zero immediately
window->adjustSize (); // resize the window
label->setMinimumSize (0, 0); // allow shrinking afterwards
like image 39
gregn3 Avatar answered Sep 20 '22 02:09

gregn3