Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to remove the margin between QWidgets in layout
QtWS25 Last Chance

How to remove the margin between QWidgets in layout

Scheduled Pinned Locked Moved General and Desktop
layout margin s
6 Posts 3 Posters 4.6k Views
  • 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 Offline
    D Offline
    DDWW
    wrote on last edited by p3c0
    #1

    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.

    p3c0P 1 Reply Last reply
    0
    • D DDWW

      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.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

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

      157

      D 1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        mainLayout->setSpacing(0);
        
        1 Reply Last reply
        0
        • D Offline
          D Offline
          DDWW
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • p3c0P p3c0

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

            D Offline
            D Offline
            DDWW
            wrote on last edited by
            #5

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

            p3c0P 1 Reply Last reply
            0
            • D DDWW

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

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

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

              157

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved