How to remove the margin between QWidgets in layout



  • mainLayout->setContentsMargins(0,0,0,0);
    This can't remove the margin in the center.

    here is my code:

        QWidget* titleBar = new QWidget(this);
        QHBoxLayout*    hLayout = new QHBoxLayout();
        QLabel* mTitle = new QLabel("hgis");
        hLayout->addWidget(mTitle);
        hLayout->addStretch();
        titleBar->setLayout(hLayout);
        titleBar->setStyleSheet("background-color:blue;");
    
        //
        QMainWindow* coreWidget = new QMainWindow(this);
        coreWidget->menuBar()->addAction(new QAction(QString("test1"),this));
        coreWidget->setWindowFlags(Qt::FramelessWindowHint);
        coreWidget->setStyleSheet("background-color:black;margin: 0px;");
    
        //
        QVBoxLayout* mainLayout = new QVBoxLayout();
        mainLayout->addWidget(titleBar);
        mainLayout->addWidget(coreWidget);
        mainLayout->setContentsMargins(0,0,0,0);
    
        setLayout(mainLayout);
        setStyleSheet("background-color:red;");
    

    I want to remove the red line between QLabel and QMainwindow.


  • Moderators

    Hi @DDWW and Welcome,
    setSpacing for mainLayout to 0.


  • Moderators

    Margin is the space around the content. Item spacing is controlled like this:

    mainLayout->setSpacing(0);
    


  • 3Q very mush.I have been troubled by this problem for a long time.



  • @p3c0
    And can I use qss to have a similar effect?


  • Moderators

    @DDWW Stylesheets cannot be applied to QLayouts. So no way doing that. But if you dont use layout then setting min-height for both widgets may work.


Log in to reply
 

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