How to set the size percentage of a QWidget



  • 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



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

    which means, height and width are reduced by 50%



  • 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



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



  • 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



  • 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



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



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



  • 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



  • 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();
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.