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. How to keep child qwidget aspect ratio.
Forum Updated to NodeBB v4.3 + New Features

How to keep child qwidget aspect ratio.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.6k Views 2 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.
  • D Offline
    D Offline
    doodle
    wrote on last edited by doodle
    #1

    I know this question has been answered many times, but there is no one satisfied to me,

    I hope that the aspect = width/height == 1.5, and the code is below.

    QSize size = widget->size();
    
    float aspect = 1.5;
    int hintHeight = sz.height() * aspect;
    if(size.width() > hintHeight ) {
      size.rwidth() = hintHeight ;
    } else {
      size.rheight() = size.width() / aspect;
    }
    widget->resize(size);
    

    I have try this in resizeEvent() but the resize() will cause infinity loop, and the heightForWidth(int w) solution can't be applied in this situation, since I need the height depend on width when height is longer than width, and width depend on height when width is longer than height.
    I need that work like the gif in the link
    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      That links is not a git but a gif. What exactly is your child widget ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        doodle
        wrote on last edited by
        #3

        Sorry, I typed wrong, I mean I need the child widget could scale like the picture in the gif.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you show how you are handling this currently ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            doodle
            wrote on last edited by
            #5

            The gif is a photo viewer software in win10, I need a qt widget work like that, I haven't handle this yet.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              I implemented this back in the days when I was learning Qt (so be aware the code quality is probably below par and might be bugged):

              • header
              • source

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              3
              • D Offline
                D Offline
                doodle
                wrote on last edited by
                #7

                Thank you, that's work!

                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