PDA

View Full Version : Overlapping text in QStatusBar



anirbanjoy
28th March 2011, 11:56
Hi,

I have an applciation with a simple status bar.

6163

The issue is whenever I resize the main window, the texts in the status bar are overlapping with each other.

6164

I want them to be a nicely cropped like any native windows application (e.g. Windows Word). Any idea on how to achieve that?

Following code shows how I initialize the text in this status bar.



void SSDMainWindow::createStatusBar()
{
statusLabel = new QLabel(tr("Ready"));
statusLabel->setIndent(3);
statusBar()->addWidget(statusLabel);

gridSpacingLabel = new QLabel(tr(" Grid Spacing: "));
gridSpacingLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
gridSpacingLabel->setMinimumSize(gridSpacingLabel->sizeHint());
statusBar()->addPermanentWidget(gridSpacingLabel);
//statusBar()->addWidget(gridSpacingLabel);
gridSpacingLabel->setText(tr(" Grid Spacing:"));

xPosLabel = new QLabel(tr(" X: 640 "));
xPosLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
xPosLabel->setMinimumSize(xPosLabel->sizeHint());
statusBar()->addPermanentWidget(xPosLabel);
// statusBar()->addWidget(xPosLabel);
xPosLabel->setText(tr(" X:"));

yPosLabel = new QLabel(tr(" Y: 480 "));
yPosLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
yPosLabel->setMinimumSize(yPosLabel->sizeHint());
statusBar()->addPermanentWidget(yPosLabel);
// statusBar()->addWidget(yPosLabel);
yPosLabel->setText(tr(" Y:"));

widthLabel = new QLabel(tr(" Width: 640 "));
widthLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
widthLabel->setMinimumSize(widthLabel->sizeHint());
statusBar()->addPermanentWidget(widthLabel);
// statusBar()->addWidget(widthLabel);
widthLabel->setText(tr(" Width:"));

heightLabel = new QLabel(tr(" Height: 480 "));
heightLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
heightLabel->setMinimumSize(heightLabel->sizeHint());
statusBar()->addPermanentWidget(heightLabel);
// statusBar()->addWidget(heightLabel);
heightLabel->setText(tr(" Height:"));

unsavedDataLabel = new QLabel();
QPixmap pixmap(":changes.png");
unsavedDataLabel->setPixmap(pixmap);
unsavedDataLabel->setMinimumSize(unsavedDataLabel->sizeHint());
statusBar()->addPermanentWidget(unsavedDataLabel);
// statusBar()->addWidget(unsavedDataLabel);
unsavedDataLabel->clear();

if (statusBarManager) {
statusBarManager->connectUnsavedData(this, SLOT(unsavedData(bool)));
statusBarManager->connectPositionChanged(this, SLOT(positionChanged(qreal, qreal)));
statusBarManager->connectSizeChanged(this, SLOT(sizeChanged(qreal, qreal)));
statusBarManager->connectGridSpacingChanged(this, SLOT(gridSpacingChanged(int)));
}
}


Regards,
Anirban

high_flyer
28th March 2011, 13:14
Then you have to avoid setting minimumSize on the labels.

anirbanjoy
29th March 2011, 09:11
Thanks high_flyer,

That removed the overlapping. But still the texts are getting cramped into each other.

6168

I want the QLabels to maintain their width, and, if resized, they should go out of sight. Like it happens in MS Word or any other native MS app.

Regards,
Anirban

high_flyer
29th March 2011, 09:39
I see.
Well, the current behavior is due to the fact that the QStatusBar has a layout inside it.
But you want it with a fixed minimum (the minimum being the combined length of all the displayed information in it).
I would therefore try to set a minimum width to the status bar.

anirbanjoy
29th March 2011, 13:34
Unfortunately does not solve the problem.

The proposed solution limits the minimum width of the whole application plus I do not see this hiding-of-text-on-resizing-application (as it works on native windows apps).

high_flyer
29th March 2011, 13:43
Hmm... maybe some one else can help you more.
I think this is not possible with QStatusBar, without sub classing, and overwriting the layout behavior.

anirbanjoy
31st March 2011, 08:26
I have some more inputs. It looks like a problem of base class QMainWindow. For the same code, Qt3 and Qt4 behaves in a different manner.

Qt3:
- beheaves like native windows apps. QStatusBar::addPermanentWidget really makes the widget permamanent. The user can not resize the main window in a way so that the permanent widgets are hidden.

6176

Qt4:
- On resizing certain texts of the supposed-to-permamnent-widget becomes hidden.

6177

It's not recommneded to go back to Qt3. So, I need to find out how to take care of this issue in Qt4.

I'm using the portedcanvas Qt example to single out this problem. I added a private method createStatusBar() in canvas.cpp:


void Main::createStatusBar()
{
statusLabel = new QLabel(tr("Ready"));
gridSpacingLabel = new QLabel(tr(" Grid Spacing: 20"));
xPosLabel = new QLabel(tr(" X: 640 "));
yPosLabel = new QLabel(tr(" Y: 640 "));
widthLabel = new QLabel(tr(" Width: 640 "));
heightLabel = new QLabel(tr(" Height: 480 "));
unsavedDataLabel = new QLabel();
QPixmap pixmap(":changes.png");
unsavedDataLabel->setPixmap(pixmap);

statusBar()->addPermanentWidget(gridSpacingLabel);
statusBar()->addPermanentWidget(xPosLabel);
statusBar()->addPermanentWidget(yPosLabel);
statusBar()->addPermanentWidget(widthLabel);
statusBar()->addPermanentWidget(heightLabel);
statusBar()->addPermanentWidget(unsavedDataLabel);

statusBar()->addWidget(statusLabel);
statusBar()->showMessage(tr("Showing Message"));
}

This method is called at the end of constructor. Also note the small code change to switch between Qt3 and Qt4 [ Q"3"MainWindow(parent,name,f) ]



Main::Main(QGraphicsScene& c, QWidget* parent, const char* name, Qt::WindowFlags f) :
Q3MainWindow(parent,name,f),
//QMainWindow(parent,name,f),
canvas(c)
{
editor = new FigureEditor(canvas,this);
QMenuBar* menu = menuBar();

Q3PopupMenu* file = new Q3PopupMenu( menu );
file->insertItem("&Fill canvas", this, SLOT(init()), Qt::CTRL+Qt::Key_F);
file->insertItem("&Erase canvas", this, SLOT(clear()), Qt::CTRL+Qt::Key_E);
file->insertItem("&New view", this, SLOT(newView()), Qt::CTRL+Qt::Key_N);
file->insertSeparator();
file->insertItem("&Print...", this, SLOT(print()), Qt::CTRL+Qt::Key_P);
file->insertSeparator();
file->insertItem("E&xit", qApp, SLOT(quit()), Qt::CTRL+Qt::Key_Q);
menu->insertItem("&File", file);

Q3PopupMenu* edit = new Q3PopupMenu( menu );
edit->insertItem("Add &Circle", this, SLOT(addCircle()), Qt::ALT+Qt::Key_C);
edit->insertItem("Add &Hexagon", this, SLOT(addHexagon()), Qt::ALT+Qt::Key_H);
edit->insertItem("Add &Polygon", this, SLOT(addPolygon()), Qt::ALT+Qt::Key_P);
edit->insertItem("Add Spl&ine", this, SLOT(addSpline()), Qt::ALT+Qt::Key_I);
edit->insertItem("Add &Text", this, SLOT(addText()), Qt::ALT+Qt::Key_T);
edit->insertItem("Add &Line", this, SLOT(addLine()), Qt::ALT+Qt::Key_L);
edit->insertItem("Add &Rectangle", this, SLOT(addRectangle()), Qt::ALT+Qt::Key_R);
edit->insertItem("Add &Sprite", this, SLOT(addSprite()), Qt::ALT+Qt::Key_S);
edit->insertItem("Create &Mesh", this, SLOT(addMesh()), Qt::ALT+Qt::Key_M );
edit->insertItem("Add &Alpha-blended image", this, SLOT(addButterfly()), Qt::ALT+Qt::Key_A);
menu->insertItem("&Edit", edit);

Q3PopupMenu* view = new Q3PopupMenu( menu );
view->insertItem("&Enlarge", this, SLOT(enlarge()), Qt::SHIFT+Qt::CTRL+Qt::Key_Plus);
view->insertItem("Shr&ink", this, SLOT(shrink()), Qt::SHIFT+Qt::CTRL+Qt::Key_Minus);
view->insertSeparator();
view->insertItem("&Rotate clockwise", this, SLOT(rotateClockwise()), Qt::CTRL+Qt::Key_PageDown);
view->insertItem("Rotate &counterclockwise", this, SLOT(rotateCounterClockwise()), Qt::CTRL+Qt::Key_PageUp);
view->insertItem("&Zoom in", this, SLOT(zoomIn()), Qt::CTRL+Qt::Key_Plus);
view->insertItem("Zoom &out", this, SLOT(zoomOut()), Qt::CTRL+Qt::Key_Minus);
view->insertItem("Translate left", this, SLOT(moveL()), Qt::CTRL+Qt::Key_Left);
view->insertItem("Translate right", this, SLOT(moveR()), Qt::CTRL+Qt::Key_Right);
view->insertItem("Translate up", this, SLOT(moveU()), Qt::CTRL+Qt::Key_Up);
view->insertItem("Translate down", this, SLOT(moveD()), Qt::CTRL+Qt::Key_Down);
view->insertItem("&Mirror", this, SLOT(mirror()), Qt::CTRL+Qt::Key_Home);
menu->insertItem("&View", view);

menu->insertSeparator();

Q3PopupMenu* help = new Q3PopupMenu( menu );
help->insertItem("&About", this, SLOT(help()), Qt::Key_F1);
help->setItemChecked(dbf_id, TRUE);
menu->insertItem("&Help",help);

createStatusBar();

setCentralWidget(editor);

printer = 0;

init();
}

Anyone else? any other idea?

Regards,
Anirban