0

I have a QStatusBar. I've added a label, showing an icon there. Now I want to emulate the behavior of QStatusBar::showMessage() by setting text in another label I've added as a widget to status bar. I need this because I can't set padding of standard message in status bar. Otherwise the icon and text are overlapped. Now I want the QLabel containing message text expand to the full width of status bar. How can I do that?

Thanks.

vian
  • 811
  • 2
  • 12
  • 27

1 Answers1

1

Here's something you could try, I never tried on status bar so I don't know whether it works or not but I did try on other containers and works great. Create a HBoxLayout, lay out the status bar using it, add the icon and the label to it, and set the layoutStretch to 0, 1 (addStretch(0); addStretch(1)), 0 for icon meaning it will stretch to fit the icon, and 1 it will stretch to all remaining space making the label expanding to full width.

The code will look something like this:

QHBoxLayout *layout = new QHBoxLayout(statusBar);
layout->setContentsMargins(11, 11, 11, 11);
statusBar->setLayout(layout);
layout->addStretch(0);
layout->addWidget(iconlabel);
layout->addStretch(1);
layout->addWidget(textlabel);

Sorry if there are compile errors, can't try it now. Hope that helps.

EDIT: Despite the fact that the upper code is not workin I won't remove it, becouse it's the proper way for other containers. For status bar this should work:

statusBar->addWidget(iconLabel, 0);
statusBar->addWidget(textLabel, 1);
Máthé Endre-Botond
  • 4,826
  • 2
  • 29
  • 48
  • the message appears when I try to set layout: QLayout: Attempting to add QLayout "" to QStatusBar "", which already has a layout QWidget::setLayout: Attempting to set QLayout "" on QStatusBar "", which already has a layout – vian Apr 23 '11 at 21:06
  • One thing I note that setting parent on the constructor already sets layout and you don't need to call setLayout explicitly. – vian Apr 23 '11 at 21:14
  • The result is - when I set text first - it fits. But when I set longer text afterwards, it appears cut. – vian Apr 23 '11 at 21:37
  • 1
    Ok, so it seems status bar has a built in layout manager, and you should give the stretch in the addWidget function call. See the edits in the post. – Máthé Endre-Botond Apr 24 '11 at 01:02