QStatusBar remove padding

752 Views Asked by At

I want to add a colored Widget over the full QStatusBar. I added a QLabel with red background-color but there is a padding around the label, which i can't remove.

what i tried:

  • setSizeGripEnabled(false)
  • setStyleSheet("QStatusBar { border: 0px; padding: 0px; margin: 0px; }" "QStatusBar::item { border: 0px; padding: 0px; margin: 0px; }"
  • layout()->setContentsMargins(0, 0, 0, 0);

Example in different Colors to differentiate areas

Update: Example Code:

  QWidget *w = new QWidget;
  QHBoxLayout *layout = new QHBoxLayout;
  QStatusBar *statusBar = new QStatusBar;
  QLabel *label = new QLabel("Example");

  w->setStyleSheet("background-color: green");

  label->setStyleSheet("background-color: red");

  statusBar->addPermanentWidget(label, 1);
  statusBar->layout()->setContentsMargins(0, 0, 0, 0);
  statusBar->setSizeGripEnabled(false);

  setStatusBar(statusBar);

  w->setLayout(layout);
  setCentralWidget(w);
}
2

There are 2 best solutions below

3
mugiseyebrows On

I think it is not possible without pointer hacking or reimplementing all QStatusBar functionality because QStatusBar implementation based on pimpl idiom, which means some implementation hidden in private headers and borders between QStatusBar widget and children widgets are hardcoded in qstatusbar.cpp

QRect ir = item->w->geometry().adjusted(-2, -1, 2, 1);
...
QStyleOption opt(0);
opt.rect = ir;
...
style()->drawPrimitive(QStyle::PE_FrameStatusBarItem, &opt, &p, item->w);
0
fatfatson On
QStatusBar{         
        min-height: 20px;
}

use the min-height css property.