QEvent::ToolTip behavior

  • Hi,

    I have created a simple qt gui project with one push button and a tooltip to try to catch QEvent::ToolTip

    As per "QT event type doc ":http://qt-project.org/doc/qt-5.0/qtcore/qevent.html#Type-enum ToolTip should be emitted when a tooltip is requested.

    In my test program ToolTip event is emitted only when the tool is destroyed.

    Here are my files:

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QHelpEvent>

    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    bool event(QEvent *event);

    Ui::MainWindow *ui;

    #endif // MAINWINDOW_H@

    @#include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWindow w;

    return a.exec&#40;&#41;;


    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    delete ui;

    bool MainWindow::event(QEvent *event)
    if (event->type() == QEvent::ToolTip ) qDebug("ToolTip");


    Is it the intended behavior or is it a bug?

  • If what you want is to show a tooltip, then use QWidget::setTooltip() on your button. That is, let the Qt event handling work without your intervention.

    If what you want is to understand what is going on, I really don't have an answer. Hasn't your test program short-circuited the normal Qt event handling, by not calling the base class event handler? And the return type is bool, but you haven't explicitly returned True or False, what does the handler return and what does that mean to Qt, for example, that the event was not handled and it should be delegated to your window's parent?

    A search shows Qt tooltips might be platform dependent, that is, depend on the operating system? Have you disabled tooltips for all your windows?

  • Moderators

    In this way you are getting tooltip requests for the main window, not the button. It is probably at the end because you pause your mouse somewhere over the window while you reach the "x" button.
    Also you should call the default implementation inside the event() override to let Qt properly handle all the events you are not interested in handling yourself. Don't forget the return value too. Otherwise you might be facing very weird bahaviour if the events are not processed properly.

    Your approach is quite error prone. The tooltip is requested on the button so if you want to handle it in the main window a good way to do this is to install event filter like this:
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)

    //install the filter


    //and then override eventFilter...
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    if(obj == ui->button && event->type() == QEvent::ToolTip)
    //do stuff...

    return false; //let Qt handle the event further


Log in to reply