Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

How set QGridLayout's fixed size?



  • I created gridLayout, but it fits by default to the size of parent QDialog(which is very big), can't find proper function to set size of layout.

    ->setGeometry not working, and it's unusual because it accept only QRect as a param(strange).
    ->setVerticalSpacing also do nothing, i play with numbers, nothing changed.
    I tried few more, but can't remember them now, here is those which at least do some effect:

    ->setSizeConstraint() - with fixed or minimum options it changes my layout from default(dumb) size, to normal, so normal spacing between rows and all. BUT. It change whole parent QDialog :DDD

    ->setMargins do stuff, but there is no customisation, i did'nt found any ->setLeftMargin, or for other side, but such functions would do the thing.



  • Hi Engelard,
    Did you try to use QSpacerItem?
    Also, maybe you could provide us with some code to better understand what you try to achieve as well as where and why it doesn't work.



  • @ValentinMichelet No. I just want simple set size function for Label, so i would tell where is exactly borders of that label should be. No code will help you because my question is in lowest level of simplicity. But if you rly need:

    topLayout = new QGridLayout(resultsDialog);
    

    Size of resultsDialog 350200, size of topLayout is same, and i wish resize it to 50150, but not with help of setMargins, i need it be at left top corner of a widget.



  • OK, I'm not sure to fully understand your needs nor to fully know how Qt layouts work behind the scene but here are some things I would try that may be helpful (at least I hope so) but that you maybe already tried yourself:

    • Try to create a UI file to better understand how parameters work
    • Play with layoutLeftMargin (yes, you can modify left, top and so on margins of your layout from the Designer)
    • Play with constraints policies of your widgets
    • Play with your label's alignment
    • Play with layoutSizeConstraint
    • Play with Spacers (horizontal and vertical)
    • Use style sheets to actually see the border of the label (this is only a debug purpose but may help better understand what is really happening)
    • Investigate how sizeHint mechanism works, for it may be resizing automatically things behind your back

    Also, instead of some code snippets, maybe you could provide us with some sort of mockup of what you try to achieve, so that I can try to replicate it with some code?



  • I have to say that i really surprised, such simple and "first" thing that should be - is settingSize of object/widget/container, it must be an easyiest thing.



  • @ValentinMichelet said in How set QGridLayout's fixed size?:

    Play with layoutLeftMargin (yes, you can modify left, top and so on margins of your layout from the Designer)

    There is no such function, i told it when created this topic. And there is no possibility to use Designer since parent widget and layouts in it are created programmatically.

    For illustation, here is what i've got from simple code, as you can see, layout which contain 4 labels fit whole window:

    0_1540914335655_fufufu.jpg

    And size of my QGridLayout should be like that(red rect):

    0_1540914465666_fufufu.jpg



  • Again, I'm not sure to fully understand your problem (hence the mockup) so there may be simpler solutions.
    AFAIK, with only a dialog that has a grid layout and a label inside, your label will always be centered and the layout of the QDialog will occupy the entire size (hence the fact that your dialog and its layout are both 350x200). Now you can add spacers (horizontal and vertical) to push your label on the top left corner, right? Or do you really need to have a layout that is smaller than your QDialog? Also, are there other widgets inside your QDialog?



  • @ValentinMichelet said in How set QGridLayout's fixed size?:

    Again, I'm not sure to fully understand your problem

    just added an illustrations with pictures



  • @Engelard said in How set QGridLayout's fixed size?:

    just added an illustrations with pictures

    Yep, bad timing =) Thanks for the pictures!

    There is no such function, i told it when created this topic.

    I know, but what I did not know was you could not use the designer.

    From your mockup, I'd go for QSpacerItem as well as setSizePolicy/setMinimumSize/setMaximumSize methods for the label.
    Your QDialog layout's size will always fit your QDialog's size AFAIK. But maybe I'm wrong, so I'll let other people answer this if they know more about it.



  • @ValentinMichelet said in How set QGridLayout's fixed size?:

    I'd go for QSpacerItem as well as setSizePolicy/setMinimumSize/setMaximumSize methods for the label.

    No! I'm still believing in Qt! That's impossible that there is no functionality for resizing. Btw, just tried that stuff, totally no effect:

    0_1540915699536_qtttr.jpg

    P.S. label setMinimumSize/setMaximumSize change nothing(



  • I don't know, should i report a bug or something, nothing working, setsizePolicies, setMinimumHeight of labels or layout, none of that function working, i tried like 20 functions already, what sense from all that stuff if it do no effect...

    P.S. Here is the code, anybody can just insert it in your dummy project to try:

        resultsDialog = new QDialog(this, Qt::MaximizeUsingFullscreenGeometryHint | 
        Qt::WindowSystemMenuHint);
        resultsDialog->setMinimumSize(300, 450);
        resultsDialog->setWindowFlag(Qt::WindowCloseButtonHint, true);
        resultsDialog->setModal(true);
        resultsDialog->setWindowTitle("final output");
        resultsDialog->show();
    
        topRegLB = new QLabel("Most often results from  '0'  scans:", resultsDialog);
        firstRegLB = new QLabel("#1: ----", resultsDialog);
        secondRegLB = new QLabel("#2: -----", resultsDialog);
        thirdRegLB = new QLabel("#3: ------", resultsDialog);
    
        topLayout = new QGridLayout(resultsDialog);
        topLayout->addWidget(topRegLB, 0, 0, 1, 2);
        topLayout->addWidget(firstRegLB, 1, 0);
        topLayout->addWidget(secondRegLB, 2, 0);
        topLayout->addWidget(thirdRegLB, 3, 0);
    


  • After two hours i finally found something useful.

    using:

    topLayout->setRowStretch(0 | 1 | 2 | 3, 25);
    

    with combination for some widgets in layout Qt::AlignTop gives proper result:

    0_1540920893843_rowsfuck.jpg

    But layout still enormously huge, so it still does'nt change a size.



  • Oh yeah, the rowStretch is important too, I forgot. But again, the size of the QDialog’s layout is the size of the QDialog itself. I don’t see how to change that. If anyone knows, I’m more than interested.
    Would it be possible to use a child layout?



  • @ValentinMichelet said in How set QGridLayout's fixed size?:

    the size of the QDialog’s layout is the size of the QDialog itself.

    This is probably the most important combination of words in whole topic, clarify alot.



  • @Engelard said in How set QGridLayout's fixed size?:

    @ValentinMichelet said in How set QGridLayout's fixed size?:

    the size of the QDialog’s layout is the size of the QDialog itself.

    This is probably the most important combination of words in whole topic, clarify alot.

    Along with

    the layout of the QDialog will occupy the entire size (hence the fact that your dialog and its layout are both 350x200).

    and

    Your QDialog layout's size will always fit your QDialog's size AFAIK.

    Which are essentially three different ways to express the same idea ;)


Log in to reply