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. Using QML / Qt Creator to create layouts that stretch & shrink with window size? Is percentage size possible?
QtWS25 Last Chance

Using QML / Qt Creator to create layouts that stretch & shrink with window size? Is percentage size possible?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 12.7k 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.
  • Q Offline
    Q Offline
    QtQitten
    wrote on last edited by
    #1

    Hi guys,

    I've had a bit of a hunt online however I am yet to find any good examples of how to do this. I would like my UI to resize as I set it. So it could resize everything, or it could resize just some aspects so things like menu's and buttons are static in size but graphics, graphs etc resize fully. I have begun the process using anchors to anchor items like menus inside a containing item to the side and the header bar to the top and expanding width wide with the screen, however it's more complex internal componants that are proving more tricky to figure out the best approach.

    My theory is: I make all items inside the object inherit off the parent item, which inherits off the main window size. However, is there any way to use percentages? So if I have an interface with two graphs and a box with input values, can make the values box fixed then the graphs take up something like 60% for one and 40% for the other of the remaining space? In my hunting online I have yet to find an example of sizing in QML or Qt Creator using percentages.

    Appreciate any comments whether you know how to do this or are looking to do the same thing. Happy to share any methods I come up with.

    Thank you.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      in case of QML you use something like :
      @width: parent.witdh * 0.6 // to get 60%@
      in your child

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #3

        Yep, just use property binding and you are good to go. Multiplying by 1 is 100%, multiplying by 0.5 is 50%, multiplying by 0.12 is 12% and so on. It's first grade math :P

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          QtQitten
          wrote on last edited by
          #4

          Ah, why on earth didnt I think of that! Its the same as placing gradient stops.

          Thank you Eddy and Ddriver. Surprised that wasnt anywhere in my hunting. Hope this post helps others as well :)

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            QtQitten
            wrote on last edited by
            #5

            Hi,

            New query. I have now sucessfully made items which stretch and shrink with the window size (thank you for help there). My next step is to put those componants together on a page.

            I have a piece of code for each block to position it:

            anchors.top: nameofblockabove.bottom
            anchors.topMargin: 10

            In the theory it will put each block one below the other with a space of 10...

            Spot the deliberate mistake?

            It is still targeting the top of the nameofblockabove when I run it.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              Is the "blockabove" the actual component, or is a container component of some kind which might happen to have a height of 0?

              Edit to add: Incidentally, in the future, be sure to start a new thread for new questions that aren't directly related to the previous thread items.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                QtQitten
                wrote on last edited by
                #7

                I've got multiple things grouped inside items and am using those top level items to layout the content on the overall page. So the term block should have been parent item.

                If I set an item to anchor its left hand side to another items right hand side I can put them side by side, I assumed I could do the same to put things one above another. However they seem to e anchoring to the top rather than the bottom of the item above. I would like each item one above the other regardless of the size of the screen and each individual item now resizes width and height ok, just getting them to sit in relation to each other now.

                Sorry for multi-thread, was unsure as it's part of the same overall question about expanding and contracting layouts.

                Thanks for help. I'm a front end web coder by nature so sometimes it's just that I think about these things differently as my background is from a different style of coding.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  Make sure that the container items are actually the size you think they are. Items don't expand to the size of their contents. If you're not setting a height: property then this is probably what's happening.

                  If the height of the container is really zero, then its neighbor is really anchoring to the bottom, but it looks like the top. (A very quick and dirty way to check is to set "clip: true" on the container item, which will effectively hide anything which is outside the bounds of that item.)

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    QtQitten
                    wrote on last edited by
                    #9

                    Thanks, I will try that tomorrow morning - it's odd because the items inside are set to match the parent width and height - those are set as a percentage of the overall size of the screen so if the parent item is 0 the internal one ought to also display as such. I will try clip and see if perhaps I need to check how I set those, the way I am doing it works like a cascade so perhaps the issue lies with my first item.

                    Thanks again!

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      QtQitten
                      wrote on last edited by
                      #10

                      You were correct - one item near the top of the dependancies tree needed a size set (did this as a percentage of the overall screen) and the others are now responding better.

                      Thanks for help.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mlong
                        wrote on last edited by
                        #11

                        You're very welcome! Glad it wasn't anything more complex than that. Be sure and edit your original post title to add [Solved] to it. Thanks!

                        Software Engineer
                        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                        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