Reduce Vertical Spacing of My Content in QGridLayout
-
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?
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);
-
@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).
-
@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.
-
Hi,
Is your picture showing only your group box ?
-
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);
-
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);
-
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 )