HeightForWidth



  • Trying to make my top level window maintain aspect ratio when resizing :

    In my header I have :
    @int heightForWidth(int w) const ;
    QSize sizeHint() const;@

    In my cpp :

    In constructor ,
    @QSizePolicy myPolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    setSizePolicy(myPolicy);
    sizePolicy().setHeightForWidth(true);@

    @int MyClass::heightForWidth(int w) const
    {
    return w*8/10;
    }

    QSize MyClass::sizeHint() const
    {
    int w = 1000;
    return QSize( w, heightForWidth(w) );
    }@

    It's not working . heightForWidth is not being called when I resize the window .
    It appears this is a problem for a top level window . Is there any solution ?



  • I also tried creating a custom layout that implements heightForWidth and set the top window's layout to the custom layout but that didn't do anything either ; maybe i am not creating the custom layout correctly .

    Anyone have code on how to make a custom layout ?



  • ...



  • Hi, both sizeHint and heightForWidth are hints used by QLayout to control the layout of its items. It doesn't work for ToplevelWidget, as it isn't managed by a QLayout.

    From http://doc.qt.digia.com/qq/qq04-height-for-width.html

    bq. The best way to handle such forms is to allow the user to freely resize them



  • [quote author="1+1=2" date="1373509993"]

    bq. The best way to handle such forms is to allow the user to freely resize them[/quote]

    Well , that is absolutely not an option ! I must have aspect ratio maintained . I see an application that has this feature so it must be possible .



  • Yes, you can re-implement the resizeEvent() function, in which resize() can be called to adjust the windows size if you want.

    But,
    When resizeEvent() is called, the widget already has its new geometry. So maybe you need to do this in system API level if you want to get better result.



  • I call resize() within resizeEvent() ... it almost works ; just not smooth enough .
    How often is resizeEvent() triggered ? It looks like for every one pixel change ?

    Here is my code :
    @
    void MyClass::resizeEvent(QResizeEvent * event)
    {

      if(!adjusted_for_aspect_ratio)
      {
             QSize old_size = event->oldSize();
             QSize new_size = event->size();
    
             int new_width = new_size.width();
             int new_height = new_size.height();
    
             int old_width = old_size.width();
             int old_height = old_size.height();
    
             adjusted_for_aspect_ratio = true;
    
             if(new_width != old_width)
             {
                    resize(new_width,new_width*8/10);
             }
            else
            {
                   if(new_height != old_height){
                              resize(new_height*10/8,new_height);
                   }
            }
      }
    
      adjusted_for_aspect_ratio = false;
    
      resizeMyChildWidgets();
    

    }
    @

    So I avoid recursion by setting the variable adjusted_for_aspect_ratio .
    Like I said , it almost works but the transition while I drag the window edges is not smooth and sometimes the window snaps back unless I drag very slowly .

    Maybe if I understood how the resizeEvent() works better ...



  • I am going to try another approach based on mouse drag event .



  • Nothing works so far .

    What about QWindow ? Looking at the API , it seems to have more capability for resizing . Is it possible to turn my parentless QWidget into a QWindow ?



  • Maybe I will try modifying the resize function itself .



  • OK , I can see when i debug that resize is actually never called ( when I resize the window with my mouse ) .

    So far I am able to tell that drawWidget on the widget is called . What is calling drawWidget though ? Or anybody know what is the starting point code where I should place my breakpoint so that I can step through the code ; i want to see what exactly is happening when i resize from the beginning .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.