Search code examples
qtc++11user-interfaceqt5statusbar

Label on QToolBar, possible?


Is is possible to have labels in a QToolBar? Something like the green text I draw on top of the app screenshot.

I would like to have a set of actions in a toolbar, all them retated to some a system. Then another toolbar with another set of actions related to another system. So each toolbar would have a label named accordingly to the specific system.

enter image description here

QApplication a(argc, argv);
QMainWindow *w = new QMainWindow;

QToolBar *barA = new QToolBar;
barA->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

QAction *actOnA = new QAction("OnA");
actOnA->setIcon(QIcon("../../../on.png"));
barA->addAction(actOnA);
QAction *actOffA = new QAction("OffA");
actOffA->setIcon(QIcon("../../../off.png"));
barA->addAction(actOffA);
w->addToolBar(barA);

QToolBar *barB = new QToolBar;
barB->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

QAction *actOnB = new QAction("OnB");
actOnB->setIcon(QIcon("../../../on.png"));
barB->addAction(actOnB);
QAction *actOffB = new QAction("OffB");
actOffB->setIcon(QIcon("../../../off.png"));
barB->addAction(actOffB);
w->addToolBar(barB);

w->show();
return a.exec();

I think I saw similar labels in a Microsoft MFC project.


Solution

  • yes, you need to create your custom widget first and then do:

    ui->statusBar->addWidget(MY_CUSTOM_WIDGEt);
    

    e.g.

    auto b = new QPushButton(this);
    b->setText("hello");
    connect(b, &QPushButton::clicked, [](){qDebug()<< "ok...";});
    ui->statusBar->addWidget(b);