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. Fixed aspect ratio for a widget

Fixed aspect ratio for a widget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 13.2k Views
  • 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.
  • F Offline
    F Offline
    florent.revelut
    wrote on last edited by
    #1

    Hello,

    In one of my projects, I had as a business requirement (flawed or not, this is out of scope) to have a fixed aspect ratio on my top level (and unique) widget.

    Basically, user should select 4:3 or 16:9 then widget keeps aspect ratio.

    I dug into QWidget documentation and code but I didn't manage to do this nor did I find any good solution.

    I tried catching the resize event to hack ratio but it linked to some unsatisfactory results, I tried the setPreferedHeightForWidth, but it's well named, it works only in one way...

    I finally lost faith and got rid of that requirement but now that this forum exist, does any one know a clean, good way of doing it ?

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      Take a look at Qxt and in particular the "QxtLetterBoxWidget":http://libqxt.bitbucket.org/doc/tip/qxtletterboxwidget.html#details

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

        Does "heightForWidth()":http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#heightForWidth do what you need?

        1 Reply Last reply
        0
        • F Offline
          F Offline
          florent.revelut
          wrote on last edited by
          #4

          @tobias
          No it does not : it fulfills only a partial use case. Depending on the actual resize (shrinking only horizontally / only vertically - growing only vertically / only horizontally), the result can produce a wrong ratio.

          Sorry, for not having linked the exact name, setPreferedHeightForWidth was what my mind remembered of height for width....

          I'll try to make an example.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on last edited by
            #5

            You will of course also need to set height for width in the size polizy, true. I have never tried it, not being the UI kind of guy, but I am pretty sure that should work.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              florent.revelut
              wrote on last edited by
              #6

              Easy enough, just try to compile that

              @
              #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(&#41;;
              }
              

              @
              and resize the windows : it doesn't care about aspect ratio at all...

              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