summaryrefslogtreecommitdiffstats
path: root/src/widgets/doc/snippets/tooltips/main.cpp
blob: 94cc71f711890898489b43492024c160a6c00974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QtWidgets>

using SearchBar = QWidget;
using Element = QWidget;

class Window : public QMainWindow
{
public:
    Window(QWidget *parent = nullptr);

protected:
    bool event(QEvent *event) override;

private:
    Element *elementAt(QPoint pos) const {
        return nullptr;
    }

    QToolBar *fileToolBar;
    QMenu *fileMenu;

    SearchBar *searchBar;
};


Window::Window(QWidget *parent)
    : QMainWindow(parent)
{
//! [action_tooltip]
    QAction *openAction = new QAction(tr("&Open..."));
    openAction->setToolTip(tr("Open an existing file"));

    fileMenu = menuBar()->addMenu(tr("&File"));
    fileToolBar = addToolBar(tr("&File"));

    fileMenu->addAction(openAction);
    fileToolBar->addAction(openAction);
//! [action_tooltip]

//! [static_tooltip]
    searchBar = new SearchBar;
    searchBar->setToolTip(tr("Search in the current document"));
//! [static_tooltip]

    fileToolBar->addWidget(searchBar);
}

//! [dynamic_tooltip]
bool Window::event(QEvent *event)
{
    if (event->type() == QEvent::ToolTip) {
        QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
        if (Element *element = elementAt(helpEvent->pos())) {
            QToolTip::showText(helpEvent->globalPos(), element->toolTip());
        } else {
            QToolTip::hideText();
            event->ignore();
        }

        return true;
    }
    return QWidget::event(event);
}
//! [dynamic_tooltip]

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    Window w;
    return 0;
}