0

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.

Ezee
  • 4,214
  • 1
  • 14
  • 29
Hien Ngo
  • 253
  • 5
  • 18

1 Answers1

3

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);
}
Ezee
  • 4,214
  • 1
  • 14
  • 29
  • Sr but I not using button in QToolBar, I create it in .ui file. I add QAction to QToolBar in ui file. And I try eventFilter(QObject,QEvent) but tooltip not disappear. – Hien Ngo Dec 02 '14 at 08:37
  • It doesn't matter how do you add an action to a `QToolBar` using ui or from the code. – Ezee Dec 02 '14 at 08:39
  • thanks for your help,I'm trying it, but tooltip still appear. – Hien Ngo Dec 02 '14 at 08:53
  • It does work. I added a working example. You can try to test it. – Ezee Dec 02 '14 at 09:16
  • Ok, Ezee, because I not clean and rebuild. Thanks so much. – Hien Ngo Dec 02 '14 at 09:23
  • if have 1 action isn't enable, it still show tooltip. How to disappear all tooltip when action isn't enable ?. – Hien Ngo Dec 02 '14 at 10:05
  • As I said a button recreates a tooltip when an action is changed. To handle all changes install an event filter on each button and return `true` when a tooltip event comes. – Ezee Dec 02 '14 at 10:43
  • 1
    Added another example – Ezee Dec 02 '14 at 10:55