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 set the size percentage of a QWidget
QtWS25 Last Chance

How to set the size percentage of a QWidget

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 25.8k 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.
  • D Offline
    D Offline
    developer
    wrote on last edited by
    #1

    i want to set size percentages of a qwidget that how much percntage they cover in a QWidget
    i would like a easy way to do that

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on last edited by
      #2

      @setGeometry ( x, y, w * 0.5, h * 0.5 );@

      which means, height and width are reduced by 50%


      Declaration of (Platform) independence.

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

        hey this is not i want i want to set the percentage this i can do easily but percantage also changes when minimized or maximized i know i can create events but percentage will be better

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rahul Das
          wrote on last edited by
          #4

          What exactly are you trying to do ? U meant, u know how to resize on resizeevents. And you can set the geometry as well.
          And i dont know if there is much difference in saying 0.5 or 50% or 50/100.
          (Punctuation will make the lines, more readable.)


          Declaration of (Platform) independence.

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

            for this we need the dimensions and dimensions vary thats why i want percntage of width if there is a way to directly setting the percntage of parent element
            dimensions change so by your method get the deimensions and get the percentage and create a resize event but in percentage it can be direct and better

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rahul Das
              wrote on last edited by
              #6

              Initially, you'll get a width and height, once the object is up.. And if you are comparing the size of QWidget size to that of screen size (desktop), you have to get the dimensions using QDesktopWidget::screenGeometry


              Declaration of (Platform) independence.

              1 Reply Last reply
              0
              • ZlatomirZ Offline
                ZlatomirZ Offline
                Zlatomir
                wrote on last edited by
                #7

                I'm not sure that i understood correctly what you want, but i think you need the "stretch":http://qt-project.org/doc/qt-4.8/qsizepolicy.html#setVerticalStretch factor (see the horizontal too)

                I'm not exactly sure how it works, but it looks it's something like: if we have 2 widgets: first with stretch factor 1 and second with 2 the second will have 2/3 from the parent and first 1/3 (but you can play with the values and see what's close to what you want)

                https://forum.qt.io/category/41/romanian

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  developer
                  wrote on last edited by
                  #8

                  hey "Zlatomir":https://qt-project.org/member/1233
                  you understand it correctly yes its m solution i have get my solution thanks man

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    developer
                    wrote on last edited by
                    #9

                    hey Zlatomir can you give me an example i was trying it but i cant get how to can you give me a simple example of 2 or 3 widgets

                    1 Reply Last reply
                    0
                    • ZlatomirZ Offline
                      ZlatomirZ Offline
                      Zlatomir
                      wrote on last edited by
                      #10

                      You play with values, i don't know exactly what those ints mean, anyway here is a little example:
                      @int main(int argc, char** argv)
                      {
                      QApplication a(argc, argv);

                      QWidget parent(0);
                      
                      QLabel* w0 = new QLabel("red");
                      w0->setStyleSheet("QLabel { background-color : red; color : blue; }");
                      
                      QSizePolicy policy0 = w0->sizePolicy();
                      policy0.setHorizontalStretch(2);
                      w0->setSizePolicy(policy0);
                      
                      
                      QLabel* w1 = new QLabel("green");
                      w1->setStyleSheet("QLabel { background-color : green; color : blue; }");
                      
                      QSizePolicy policy1 = w1->sizePolicy();
                      policy1.setHorizontalStretch(1);
                      w1->setSizePolicy(policy1);
                      
                      QHBoxLayout* layout = new QHBoxLayout(&parent);
                      layout->addWidget(w0);
                      layout->addWidget(w1);
                      
                      parent.show();
                      return a.exec();
                      

                      }@

                      https://forum.qt.io/category/41/romanian

                      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