Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to set the size percentage of a QWidget

    General and Desktop
    3
    10
    22711
    Loading More Posts
    • 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
      developer last edited by

      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 Reply Quote 0
      • R
        Rahul Das last edited by

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

        which means, height and width are reduced by 50%


          Rahul Das
        

        1 Reply Last reply Reply Quote 0
        • D
          developer last edited by

          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 Reply Quote 0
          • R
            Rahul Das last edited by

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


              Rahul Das
            

            1 Reply Last reply Reply Quote 0
            • D
              developer last edited by

              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 Reply Quote 0
              • R
                Rahul Das last edited by

                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


                  Rahul Das
                

                1 Reply Last reply Reply Quote 0
                • Zlatomir
                  Zlatomir last edited by

                  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 Reply Quote 0
                  • D
                    developer last edited by

                    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 Reply Quote 0
                    • D
                      developer last edited by

                      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 Reply Quote 0
                      • Zlatomir
                        Zlatomir last edited by

                        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 Reply Quote 0
                        • First post
                          Last post