Force formatting when resizing the main window

I understand that in Qt4 there is no single flag that you can configure so that resizing the window in mouse mode supports a certain aspect ratio (say, 1: 1). Is there a way to force a new size for the window while in the "resizeEvent (QResizeEvent * event")? event parameters do not seem to support the change or give the user the ability to enter a new size.

My goal: resizing the GUI / mouse in a specific window will maintain the current aspect ratio, regardless of where you resize it (sides, top, bottom, corners).

+4
source share
3 answers

Duplicate question? How to keep widgets proportions in Qt?

Referring to the answer "df" in the link above

The call to resize () from resizeEvent () never worked well for me - at best, it will cause flickering when the window is resized twice (as you have), in the worst case, an infinite loop.

I think the β€œright” way to maintain a fixed aspect ratio is to create a custom layout. You will have to override only two methods, QLayoutItem :: hasHeightForWidth () and QLayoutItem :: heightForWidth ().

Also check out the Sam Dutton solution @ http://lists.trolltech.com/qt-interest/2007-01/msg00204.html

void MyWindow::resizeEvent(QResizeEvent * /*resizeEvent*/) { int containerWidth = _myContainerWidget->width(); int containerHeight = _myContainerWidget->height(); int contentsHeight = containerHeight ; int contentsWidth = containerHeight * _aspectRatio; if (contentsWidth > containerWidth ) { contentsWidth = containerWidth ; contentsHeight = containerWidth / _aspectRatio; } resizeContents(contentsWidth, contentsHeight); } 
+2
source

Quoting a message in the stream referenced by @blueskin :

This is surprisingly hard to do. Most window systems do not allow an application to set limits on a window other than max and min, and that it can be fixed.

The most common way to do this is to trigger a resize from a resize event. This often leads to recursion problems or looks weird because the widget is resized in a way that the user did not request.

Since most window systems do not allow you to resize the main window accordingly, the best solution is often to limit the child widget, rather than the parent. One way to do this is with the class provided by libqxt.

libqxt supports child widget support with a specific aspect ratio through QxtLetterBoxWidget . @blueskin's answer gives one good try to accomplish what was originally requested.

If you are interested, I recommend that you read the source for the resizeWidget() function and observe the places it falls into. As a background, libqxt uses pimpl and qxt_d() gets in the private member.

+2
source

Another quick and easy way to do this: http://developer.qt.nokia.com/forums/viewthread/5320/#31866

 #include <QWidget> #include <QApplication> #include <QSizePolicy> class MyWidget:public QWidget { public: MyWidget():QWidget(){}; ~MyWidget(){}; virtual int heightForWidth ( int w ) const { return w*9/16;}; }; int main (int argv, char** argc) { QApplication a(argv,argc); MyWidget w; QSizePolicy qsp(QSizePolicy::Preferred,QSizePolicy::Preferred); qsp.setHeightForWidth(true); w.setSizePolicy(qsp); w.show(); a.exec(); } 
+1
source

All Articles