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. Reduce Vertical Spacing of My Content in QGridLayout

Reduce Vertical Spacing of My Content in QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 719 Views 2 Watching
  • 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.
  • P Offline
    P Offline
    Phamy1289
    wrote on last edited by Phamy1289
    #1

    I have the space marked that I want to reduce. The code show's what I've done. Is there a way to reduce the top space even more?

    481d56a2-bfe6-400f-ba14-e63a0355e84a-image.png

        QGroupBox *pGroupBox = new QGroupBox;
        pGroupBox->setStyleSheet("background:gray");
    
        QGridLayout *pGridLayout = new QGridLayout;
        pGridLayout->setSpacing(0);
        pGridLayout->setMargin(0);
    
        QLabel *pHeader = new QLabel(this);
        QFont headerFont("times", 18);
        headerFont.setBold(true);
    
        pHeader->setFont(headerFont);
    
        pHeader->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        pHeader->setAlignment(Qt::AlignCenter);
        pHeader->setText("Details");
        pHeader->setStyleSheet("QLabel {color:white;}");
    
        QDateTime *myDateTime = new QDateTime;
        QDate *myDate = new QDate;
    
        myDate->setDate(2018, 3, 28);
        myDateTime->setDate(*myDate);
        myDateTime->setTime(QTime::currentTime());
    
        mpStartDateTime = new QDateTimeEdit;
        mpStartDateTime->setDisplayFormat("yyyy-MM-ddThh:mm:ss");
        mpStartDateTime->setStyleSheet("color: white;"
                                       "border:0");
        mpStartDateTime->setReadOnly(true);
        mpStartDateTime->setAlignment(Qt::AlignLeft);
        mpStartDateTime->QAbstractSpinBox::setButtonSymbols(QAbstractSpinBox::NoButtons);
    
    #ifdef QT_DEBUG
        mpStartDateTime->setDateTime(*myDateTime);
    #else
        mpStartDateTime->setDateTime(QDateTime::currentDateTime());
    #endif
    
        mpCloseDateTime = new QDateTimeEdit;
        mpCloseDateTime->setDisplayFormat("yyyy-MM-ddThh:mm:ss");
        mpCloseDateTime->setStyleSheet("color: white;"
                                       "border:0");
        mpCloseDateTime->setDateTime(QDateTime::currentDateTime());
        mpCloseDateTime->setReadOnly(true);
        mpCloseDateTime->setAlignment(Qt::AlignLeft);
        mpCloseDateTime->QAbstractSpinBox::setButtonSymbols(QAbstractSpinBox::NoButtons);
    
        QLabel *pStartDateLabel = new QLabel("Start Date");
        pStartDateLabel->setStyleSheet("QLabel {color:cyan;}");
        pStartDateLabel->setAlignment(Qt::AlignCenter);
        QLabel *pEndDateLabel = new QLabel("End Date");
        pEndDateLabel->setStyleSheet("QLabel {color:cyan;}");
        pEndDateLabel->setAlignment(Qt::AlignCenter);
    
        pGridLayout->addWidget(pHeader, 0, 0, 1, 4);
        pGridLayout->addWidget(pStartDateLabel, 1, 0);
        pGridLayout->addWidget(mpStartDateTime, 1, 1);
        pGridLayout->addWidget(pEndDateLabel, 1, 2);
        pGridLayout->addWidget(mpCloseDateTime, 1, 3);
        pGroupBox->setLayout(pGridLayout);
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Phamy1289 said in Reduce Vertical Spacing of My Content in QGridLayout:

      grid->addWidget(QDateTime, 1, 0)

      This will not compile

      Create your ui in the designer. I would guess the size policy is not properly set or the complete widget is not in a proper layout (with e.g. an expanding vertical spacer).

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @Phamy1289 said in Reduce Vertical Spacing of My Content in QGridLayout:

        grid->addWidget(QDateTime, 1, 0)

        This will not compile

        Create your ui in the designer. I would guess the size policy is not properly set or the complete widget is not in a proper layout (with e.g. an expanding vertical spacer).

        P Offline
        P Offline
        Phamy1289
        wrote on last edited by
        #3

        @Christian-Ehrlicher Sorry, I was just trying to have my code a bit easier to read. I've changed it to show the whole code.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Is your picture showing only your group box ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          P 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Is your picture showing only your group box ?

            P Offline
            P Offline
            Phamy1289
            wrote on last edited by
            #5

            @SGaist Yes, it's showing only the groupbox and the contents in it

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #6

              Your code displays nothing.
              pGroupBox seems to go nowhere ...

              Where do you put the groupBox ?

              Adding the following seems to work - for me :)

              auto layout=new QHBoxLayout;
              layout->addWidget(pGroupBox);
              layout->setContentsMargins(0,0,0,0);
              setLayout(layout);
              
              1 Reply Last reply
              0
              • P Offline
                P Offline
                Phamy1289
                wrote on last edited by
                #7

                It's in my main function.

                    QPalette backgroundColor(palette());
                    backgroundColor.setColor(QPalette::Background, Qt::black);
                
                    QGridLayout *pGrid = new QGridLayout;
                    QWidget *pBaseWidget = new QWidget;
                
                    pBaseWidget->setAutoFillBackground(true);
                    pBaseWidget->setPalette(backgroundColor);
                    pBaseWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
                    pBaseWidget->setMaximumWidth(800);
                
                    pBaseWidget->setLayout(pGrid);
                    setWidget(pBaseWidget);
                
                    //Other Stuff...
                
                    pGrid->addWidget(myGroupBoxDateTime, 0, 0, 1, 2);
                
                1 Reply Last reply
                0
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  add QSpacerItem into QGridLayout at the bottom to push all widgets up
                  void addItem(QLayoutItem * item, int row, int column, int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = 0)

                  or try to align widgets to top
                  void QGridLayout::addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::AlignTop )

                  P 1 Reply Last reply
                  1
                  • JoeCFDJ JoeCFD

                    add QSpacerItem into QGridLayout at the bottom to push all widgets up
                    void addItem(QLayoutItem * item, int row, int column, int rowSpan = 1, int columnSpan = 1, Qt::Alignment alignment = 0)

                    or try to align widgets to top
                    void QGridLayout::addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::AlignTop )

                    P Offline
                    P Offline
                    Phamy1289
                    wrote on last edited by
                    #9

                    @JoeCFD Thank you, adding the QSpacerItem did the trick.

                    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