Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. HeightForWidth

HeightForWidth

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    great88
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      great88
      wrote on last edited by
      #2

      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 ?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        great88
        wrote on last edited by
        #3

        ...

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            great88
            wrote on last edited by
            #5

            [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 .

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                great88
                wrote on last edited by
                #7

                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 ...

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  great88
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    great88
                    wrote on last edited by
                    #9

                    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 ?

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      great88
                      wrote on last edited by
                      #10

                      Maybe I will try modifying the resize function itself .

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        great88
                        wrote on last edited by
                        #11

                        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 .

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved