QStatusBar - replacing widgets from bar

  • Hi,
    I have status bar where I want to have three fields to placing there widgets.
    To have there that fields I can do that:

    QLabel labelOne("");
    QLabel labelTwo("");
    QLabel labelThree("");

    statusBar->insertWidget(0, labelOne, 1); //QStatusBar* statusBar;
    statusBar->insertWidget(1, labelTwo, 1);
    statusBar->insertWidget(2, labelThree, 1);

    I want to have function that replace one of the labels with other widget.
    Is there any possibility to do this without using pointers to that labels.
    I know that I can do this in that way:

    void setWidgetToBar(QWidget* widget, int index)
    case 0:
    case 1:
    case 2:
    ui.statusBar->insertWidget(index, widget, 1);

    But problem is that I don't have only one status bar. Having pointers to all of these labels from all status bars seems to be not good idea. Unless I will create a list of that labels. Is it a good idea or maybe there is any other way to do this?

  • Qt Champions 2017

    well, maybe you could
    statusBar->removeWidget(static_cast<QWidget *>(statusBar->children()[0]));
    That is not pretty and would hide any widget at index zero, regardless if it was the label or not.

    I have not testet it. just from looking at the source.

  • Lifetime Qt Champion


    If you have several similar status bars you should rather create a subclass where you handle this. And currently yes, there's not simple way to access a widget from the status bar itself.

    However, never use a static_cast with QObject derived class, use qobject_cast and always verify that the pointer returned is not null.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.