Remove tooltip of QAction when add it to QToolBar in Qt

3.3k views Asked by At

I added a QAction on a QToolBar, but I can't remove a tooltip from the button.

I tried to overide event, eventfilter using event->type == Qt::Tooltip but it didn't help.

Please help me.

1

There are 1 answers

8
Ezee On BEST ANSWER

Why it happens

When you add an action on a toolbar:

  1. It creates a QToolButton
  2. Calls QToolButton::setDefaultAction passing the action as an argument.
  3. This method calls setToolTip(action->toolTip());
  4. action->toolTip() returns whether tooltip or if the tooltip is empty it returns text. Thus you'll always have some tooltip on the button.

What to do

Using the explanation above you can think of lots of ways to solve the problem.

For example, when QToolbar is created (and possibly shown) use toolbar->findChildren<QToolButton*> to find the buttons:

foreach(QToolButton* button, toolbar->findChildren<QToolButton*>())
{
  button->setToolTip(QString());
}

Note: when you change a text of an action, the appropriate button will recreate the tooltip. You can use an event filter for a button to process the tooltip event.

EDIT: added an example:

Ui contains a toolbar with an action.

testwindow::testwindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    foreach(QToolButton* button, ui.mainToolBar->findChildren<QToolButton*>())
    {
        button->setToolTip(QString());
    }
}

When you change an action (text, enabling state...) a QToolButton updates a tooltip. In this case you need to prevent the tooltip appearance permanently:

testwindow::testwindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    foreach(QToolButton* button, ui.mainToolBar->findChildren<QToolButton*>())
    {
        button->installEventFilter(this);
    }
}

bool testwindow::eventFilter(QObject* o, QEvent* e)
{
    if (e->type() == QEvent::ToolTip)
    {
      return true;
    }
    return QMainWindow::eventFilter(o, e);
}