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 st开发者_高级运维atus bar. How can I do that?
Thanks.
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);
精彩评论