0

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);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

2

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);
mugiseyebrows
  • 4,138
  • 1
  • 14
  • 15
  • Thank you for your answer. What do you mean with pointer hacking? Would it be possible to show a solution for this problem? I'm sorry i am new in Qt. – Alexander Schneider Dec 22 '20 at 08:01
  • Pointer hacking is method for accessing private members using pointer arithmetic, it's gennerally a bad idea, and you'll get nasty runtime error if someone changes private headers in qt library. There is another way of achieving your goal: you can remove (hide) status bar completely and instead add QHBoxLayout with labels which acts like statusbar and set it's stretch to 0 (or implement your version of statusbar which is essentialy the same thing) – mugiseyebrows Dec 22 '20 at 08:37
  • that would be awesome without using default qstatusBar. why didn't I think that? Thank you! I upvote – Denis Turgenev Jun 24 '21 at 17:08
0
QStatusBar{         
        min-height: 20px;
}

use the min-height css property.

fatfatson
  • 796
  • 10
  • 24