8

How would you suggest to add a label on a QToolbar ? Since it only accepts action or somethings like that.

Thx

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134

3 Answers3

17

As abresas wrote, a toolbar can be populated with a QAction. The QString passed along with the QAction is used in several ways (e.g as tooltip), but with 4.8 the default is to show a toolbar button with the icon only. To show the text with the icon, set the toolbar to a Qt::ToolButtonStyle, e.g.

ui->toolBar->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
handle
  • 5,859
  • 3
  • 54
  • 82
  • I had to do `ui->mainToolBar->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );` in Qt 5.4 – zar Aug 08 '15 at 03:34
  • strangely enough the text shows up tripped in the bottom for long characters like 'g'. Any idea why that might be? – zar Aug 10 '15 at 14:47
11

The QToolBar also accepts widgets like a QLabel through the method QToolBar::addWidget.

alexisdm
  • 29,448
  • 6
  • 64
  • 99
-2

You just pass the string to addAction method.

http://doc.qt.io/qt-5/qtoolbar.html

QAction *   addAction ( const QString & text )
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
abresas
  • 835
  • 6
  • 7