Set fixed margin/spacing/padding between Widgets/Layouts



  • I'm trying to figure out how to set a fixed size to margin/spacing/padding between widgets/layouts. Maybe I'm wrong. What I've is this:
    alt text

    I want to take control of that blue space in the image. I tried setting margin/spacing/padding to 0 but always there is that space!


  • Lifetime Qt Champion

    Hi,

    From the looks of it it seems you are using a stylesheet, is that the case ?



  • @SGaist
    yes! This is .css:

    #minimaxBtn {
        border: 0px solid;
        color: #fff;
        font-weight: bold;
        font-family: Arial, sans-serif;
    }
    #closeBtn {
        border: 0px solid;
        color: #fff;
        font-family: Arial, sans-serif;
    }
    
    #minimaxBtn:hover {
        background-color: rgba(175, 175, 175, 0.50);
    }
    
    #closeBtn:hover {
        background-color: red;
    }
    #closeBtn:hover:pressed {
        background-color: rgba(206, 67, 67, 0.90);
    }
    
    #minimaxBtn:hover:pressed {
        background-color: rgba(38, 38, 38, 0.50);
    }
    
    Widget { //this is the main widget
        background-color: #3d3d3d;
    }
    
    #toolBar { //this is the widget with HBoxLayout
        background-color: rgba(206, 67, 67, 0.90);
        border: 1px solid;
        border-color: aqua;
        border-radius: 0 0 0 0;
        padding: 0px 0px 0px 0px;
    }
    

  • Lifetime Qt Champion

    Did you already set the spacing and margin on the layout ?



  • @SGaist said in Set fixed margin/spacing/padding between Widgets/Layouts:

    Did you already set the spacing and margin on the layout

    Yes, I added all of them and nothing is changed!

    Widget {
        background-color: #3d3d3d;
        border: 1px solid;
        border-color: aqua;
    
        padding: 0 0 0 0;
        border-spacing: 0px 0px;
        margin: 0px;
    }
    
    #toolBar {
        background-color: rgba(206, 67, 67, 0.90);
        border: 1px solid;
        border-color: aqua;
    
        padding: 0 0 0 0;
        border-spacing: 0px 0px;
        margin: 0px;
    }
    

    I'm actually using this project as base widow to enable windows aero features:
    https://github.com/dfct/TrueFramelessWindow

    what I can see in the code is this:

    //Clear margins & spacing & add the layout to prepare for the MainAppWidget
    setContentsMargins(0, 0, 0, 0);
    setLayout(&m_Layout);
    m_Layout.setContentsMargins(0, 0, 0, 0);
    m_Layout.setSpacing(0);
    

    before adding the main widget, but what I understand that is for base aero wrapper, then I get margin in my main widget and in my toolbar, so I think this is not the problem...


  • Moderators

    @wrekler you have to set the margin and spacing of the QLayout item to 0 as well, not only in the StyleSheet. I believe those are 2 different pairs of shoes.



  • To clarify what @J-Hilk is saying (which I believe to be correct): a QLayout is not a QWidget and so cannot have its attributes affected via a stylesheet rule (sadly). That has to be done via explicit, back-end code.



  • @J.Hilk said in Set fixed margin/spacing/padding between Widgets/Layouts:

    @wrekler you have to set the margin and spacing of the QLayout item to 0 as well, not only in the StyleSheet. I believe those are 2 different pairs of shoes.

    @JonB said in Set fixed margin/spacing/padding between Widgets/Layouts:

    To clarify what @J-Hilk is saying (which I believe to be correct): a QLayout is not a QWidget and so cannot have its attributes affected via a stylesheet rule (sadly). That has to be done via explicit, back-end code.

    Here we go! That was the problem! Layout cannot be affected by a stylesheet file! Now I know how styling layout works! Thank!