Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] setToolTip in QAction menu

[Solved] setToolTip in QAction menu

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 15.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anselmolsm
    wrote on 21 Jun 2011, 03:29 last edited by
    #2

    Matheus,

    Feel free to join us in the "Brazilian group":http://developer.qt.nokia.com/groups/qt_brazil and to post your questions in the associated "forum":http://developer.qt.nokia.com/forums/viewforum/34 , where you can write in Portuguese.

    The solution proposed in that QtCentre thread you mentioned is something like this:

    @
    // Subclass QMenuBar
    class MyMenuBar : public QMenuBar
    {
    public:
    MyMenuBar(QWidget * parent = 0)
    : QMenuBar(parent) {}

    // reimplement the method event()
    virtual bool event (QEvent * e) {
        QMenuBar::event(e);
    
        const QHelpEvent *helpEvent = static_cast<QHelpEvent *>(e);
    
        // Look for QEvent::ToolTip
        if (helpEvent->type() ==QEvent::ToolTip) {
            // "call QToolTip::showText on that QAction's tooltip."
            QToolTip::showText(helpEvent->globalPos(), activeAction()->toolTip());
        } else {
            QToolTip::hideText();
        }
    }
    

    };

    int main( int argc, char **argv )
    {
    QApplication app( argc, argv );
    MyMenuBar *bar = new MyMenuBar;
    QAction *action = bar->addAction("my action");
    action->setToolTip("my tooltip");

    bar->show();
    
    return app.exec&#40;&#41;;
    

    }
    @

    DevNet colleagues, any better solution? :-)

    Anselmo L. S. Melo (anselmolsm)

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Exotic_Devel
      wrote on 21 Jun 2011, 13:55 last edited by
      #3

      I created my own version of QMenuBar, but I'm having a lot of compilation errors.

      ,h

      @#ifndef MODQMENUBAR_H
      #define MODQMENUBAR_H

      #include <QMenuBar>

      class ModQMenuBar : QMenuBar
      {

      public:
      ModQMenuBar(QWidget *parent=0);

      virtual bool event(QEvent *);
      

      }

      #endif // MODQMENUBAR_H@

      .cpp

      @#include "ModQMenuBar.h"
      #include <QHelpEvent>
      #include <QToolTip>
      ModQMenuBar::ModQMenuBar(QWidget *parent):QMenuBar(parent)
      {
      ModQMenuBar::event(QEvent *evt)
      {
      QMenuBar::event(evt);

          const QHelpEvent *helpevent = static_cast<QHelpEvent *>(e);
      
          if(helpevent->type() == QEvent.ToolTip)
          {
              QToolTip.showText(helpevent->globalPos(), activeAction()->toolTip());
          }
          else
          {
              QToolTip.hideText();
          }
      
      }
      

      };@

      error

      @
      /usr/include/QtCore/qcoreevent.h:53: error: expected initializer before ‘QtCoreModule’
      /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/ModQMenuBar.cpp:-1: In constructor ‘ModQMenuBar::ModQMenuBar(QWidget*)’:
      /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/ModQMenuBar.cpp:7: error: expected primary-expression before ‘*’ token
      /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/ModQMenuBar.cpp:7: error: ‘evt’ was not declared in this scope
      /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/ModQMenuBar.cpp:8: error: expected ‘;’ before ‘{’ token
      /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/ModQMenuBar.cpp:23: error: expected ‘}’ at end of input
      @

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Exotic_Devel
        wrote on 21 Jun 2011, 14:57 last edited by
        #4

        I managed to settle in shares

        lacked the ';' in the declaration of the class. h

        I was defining the Event method in the class constructor.

        Well now it is with the fixes that.

        @#include "ModQMenuBar.h"
        #include <QHelpEvent>
        #include <QToolTip>

        ModQMenuBar::ModQMenuBar(QWidget *parent):QMenuBar(parent)
        {

        }

        ModQMenuBar::event(QEvent * evt)
        {
        QMenuBar::event(evt);

        const QHelpEvent *helpevent = static_cast<QHelpEvent *>(e);
        
        if(helpevent->type() == QEvent.ToolTip)
        {
            QToolTip.showText(helpevent->globalPos(), activeAction()->toolTip());
        }
        else
        {
            QToolTip.hideText();
        }
        

        }@

        .h

        @#ifndef MODQMENUBAR_H
        #define MODQMENUBAR_H

        #include <QMenuBar>

        class ModQMenuBar : QMenuBar
        {

        public:
        ModQMenuBar(QWidget *parent=0);

        //virtual bool event(QEvent *);
        

        };

        #endif // MODQMENUBAR_H@

        now to two errors

        /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/ModQMenuBar.cpp:10: error: ISO C++ forbids declaration of ‘event’ with no type

        /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/ModQMenuBar.cpp:10: error: no ‘int ModQMenuBar::event(QEvent*)’ member function declared in class ‘ModQMenuBar’

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jim_kaiser
          wrote on 21 Jun 2011, 16:50 last edited by
          #5

          Except the constructor and destructor all the functions need to be defined with a return type. Of course any method defined in your implementation needs to be declared in the header.
          Uncomment this line

          @ virtual bool event(QEvent *); @

          Change the function in the .cpp to

          @ bool ModQMenuBar::event(QEvent * evt) @

          Maybe you also need a

          @ #include <QEvent> @

          That should fix the compilation.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Exotic_Devel
            wrote on 21 Jun 2011, 17:24 last edited by
            #6

            Now is functioning

            @#ifndef MODQMENUBAR_H
            #define MODQMENUBAR_H

            #include <QMenuBar>
            #include <QEvent>

            class ModQMenuBar : QMenuBar
            {

            public:
            ModQMenuBar(QWidget *parent=0);

            virtual bool event(QEvent *);
            

            };

            #endif // MODQMENUBAR_H
            @

            .cpp

            @#include "ModQMenuBar.h"
            #include <QToolTip>
            #include<QHelpEvent>

            ModQMenuBar::ModQMenuBar(QWidget *parent):QMenuBar(parent)
            {

            }

            bool ModQMenuBar::event(QEvent * evt)
            {
            const QHelpEvent *helpEvent = static_cast<QHelpEvent *>(evt);

            if(evt->type() == QEvent::ToolTip)
            {
                QToolTip::showText(helpEvent->globalPos(), activeAction()->toolTip());
            }
            else
            {
                return QMenuBar::event(evt);
            }
            return true;
            

            }
            @

            Thanks to everybody who responded

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Exotic_Devel
              wrote on 21 Jun 2011, 17:53 last edited by
              #7

              I am now trying to set this menu bar modified to be the hem of my QMainWindow setMenuBar using the inherited method () but keep getting an error on the line where ModQMenuBar instantiate.

              @ modmenubar = new ModQMenuBar(this);
              setMenuBar(modmenubar); @

              I get an error that is inaccessible QWidget.

              /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/WindowMain.cpp:25: error: ‘QWidget’ is an inaccessible base of ‘ModQMenuBar’

              1 Reply Last reply
              0
              • A Offline
                A Offline
                anselmolsm
                wrote on 21 Jun 2011, 18:16 last edited by
                #8

                In the declaration of ModQMenuBar you inherited QMenuBar, however did not make it public. In the line 7 of your header file, try the following:

                @
                class ModQMenuBar : public QMenuBar
                @

                Anselmo L. S. Melo (anselmolsm)

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jim_kaiser
                  wrote on 21 Jun 2011, 18:32 last edited by
                  #9

                  Right... you're missing the "public" keyword. When you inherit from a class, you specify the type of inheritance. Mostly you would only need public inheritance, you could read up on private and protected inheritance if you want.

                  So it should be..

                  @ class ModQMenuBar : public QMenuBar @

                  [ Edit: We probably need a "svn update" functionality on the "reply page" :-P .. so we can update the post to see if it's been answered before committing the reply.. ]

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    Exotic_Devel
                    wrote on 21 Jun 2011, 18:50 last edited by
                    #10

                    Now produced another error

                    /usr/include/QtGui/qmenubar.h:340: error: ‘QMenuBar& QMenuBar::operator=(const QMenuBar&)’ is private

                    /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/ModQMenuBar.h:8: error: within this context

                    /home/MatheusDevel/Devel/QtOptica/QtOptica-build-desktop/../QtOptica/WindowMain.cpp:25: synthesized method ‘ModQMenuBar& ModQMenuBar::operator=(const ModQMenuBar&)’ first required here

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jim_kaiser
                      wrote on 21 Jun 2011, 18:59 last edited by
                      #11

                      You wouldn't need to access the "=" operation of the menubar normally. So i in your code

                      @ modmenubar = new ModQMenuBar(this); @

                      modmenubar should a pointer to ModQMenuBar

                      @ ModQMenuBar* modmenubar; @

                      If that doesn't fix it.. could you post relevant code?

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        Exotic_Devel
                        wrote on 21 Jun 2011, 19:28 last edited by
                        #12

                        I declared in header

                        @#ifndef WINDOWMAIN_H
                        #define WINDOWMAIN_H

                        #include <QMainWindow>
                        #include <QMdiArea>
                        #include "ModQMenuBar.h"

                        class WindowMain : public QMainWindow
                        {
                        public:
                        WindowMain(QWidget *parent = 0);

                            ~WindowMain();
                        
                        private:
                            // Area MDI
                            QMdiArea *mdiarea;
                        
                            // Menus
                            QMenu *arquivos;
                            QMenu *relatorios;
                            QMenu *ferramentas;
                            QMenu *fiscal;
                            QMenu *moviment;
                            QMenu *configura;
                            QMenu *ajuda;
                            QMenu *redundantes;
                            QMenu *relgerencias;
                        
                            // QActions de menus
                            QAction *clientes;
                            QAction *produtos;
                            QAction *forneced;
                            QAction *represen;
                            QAction *usuarios;
                            QAction *medicos;
                            QAction *sair;
                            QAction *enderecos;
                            QAction *marclente;
                            QAction *matelente;
                            QAction *tipopgto;
                            QAction *tipolente;
                            QAction *tratament;
                            QAction *restricoes;
                            QAction *autobackup;
                            QAction *bdconexao;
                            QAction *impressdoc;
                            QAction *leiturax;
                            QAction *reducaoz;
                            QAction *configicms;
                            QAction *manual;
                            QAction *queisso;
                            QAction *sobre;
                            QAction *relvenda;
                            QAction *relcaixa;
                            QAction *relespelhos;
                            QAction *relos;
                            QAction *partmedica;
                            QAction *partfuncio;
                            QAction *fatumensal;
                            QAction *fatudiario;
                            QAction *ranklentes;
                            QAction *rankfornec;
                            QAction *orcamentos;
                            QAction *devolucoes;
                            QAction *movicaixa;
                            QAction *ordemservic;
                            QAction *vendas;
                            QAction *contapagar;
                            QAction *contarecebe;
                        
                            QAction *calcula;
                            QAction *callenbord;
                            QAction *compatarmlen;
                            QAction *backup;
                        
                            ModQMenuBar modmenubar; // << << << << here
                        

                        };

                        #endif // WINDOWMAIN_H
                        @

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          Exotic_Devel
                          wrote on 21 Jun 2011, 19:32 last edited by
                          #13

                          Now after posting the code here, I notice not defined as a pointer.

                          I had set as a stack variable.

                          Thanks !!!!!!!

                          @ModQMenuBar *modmenubar; // << << << << here@

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            Exotic_Devel
                            wrote on 21 Jun 2011, 19:55 last edited by
                            #14

                            Is compiling normally, but the tooltip does not yet appear.

                            I did a test by putting a message to display if the IF test is satisfied. The message is never shown, ie, the IF test is never satisfied.

                            @#include "ModQMenuBar.h"
                            #include <QToolTip>
                            #include <QHelpEvent>
                            #include <iostream>

                            ModQMenuBar::ModQMenuBar(QWidget *parent):QMenuBar(parent)
                            {

                            }

                            bool ModQMenuBar::event(QEvent * evt)
                            {
                            const QHelpEvent *helpEvent = static_cast<QHelpEvent *>(evt);

                            if(helpEvent->type() == QEvent::ToolTip)
                            {
                                QToolTip::showText(helpEvent->globalPos(), activeAction()->toolTip());
                                std::cout << "Passou no IF";           // test if
                            }
                            return QMenuBar::event(evt);
                            

                            }
                            @

                            I'm setting the tooltip for the method by QActions setToolTip

                            @restricoes = new QAction("Restrinções", configura);
                            restricoes->setToolTip("Configura o acesso dos usuários ao sistema");
                            @

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              anselmolsm
                              wrote on 21 Jun 2011, 20:59 last edited by
                              #15

                              I've just tested the code you posted, the tooltip appears.

                              Remember, to see the tooltip you need to leave the mouse pointer stopped for a while over the menu item.

                              And try to use "qDebug()":http://doc.qt.nokia.com/4.7/qdebug.html instead of std::cout.

                              Anselmo L. S. Melo (anselmolsm)

                              1 Reply Last reply
                              0
                              • E Offline
                                E Offline
                                Exotic_Devel
                                wrote on 21 Jun 2011, 21:35 last edited by
                                #16

                                ok, I'll put here some of the code where I use the ModQMenuBar

                                @
                                #include <QRect>
                                #include <QMenuBar>
                                #include <QToolBar> // Cabeçalhos da API Qt que serão usadas na janela pincipal
                                #include <QStatusBar>
                                #include <QList>

                                #include "WindowMain.h" // Usa o arquivo de cabeçalho onde a classe foi definida

                                /* Define a classe WindowMain e já invoca o
                                construtor da classe pai, passando o ponteiro
                                "parent" como parâmetro.*/

                                WindowMain::WindowMain(QWidget *parent):QMainWindow(parent)
                                {
                                QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8")); // Define os caracteres da
                                // aplicação p/ UTF-8

                                setWindowTitle("TESTETSTETS");
                                
                                modmenubar = new ModQMenuBar(this);
                                setMenuBar(modmenubar);
                                
                                
                                // Exibe a barra de status de QMainWindow
                                statusBar()->show();
                                
                                // Prepara a area de trabalho MDI da aplicação
                                mdiarea = new QMdiArea;
                                setCentralWidget(mdiarea);
                                
                                
                                // Prepara o menu principal da aplicação
                                configura                       = menuBar()->addMenu("&Configurações");
                                
                                // Prepara os itens de menu
                                restricoes                      = new QAction("Restrinções", configura);
                                autobackup                      = new QAction("Backup Automático", configura);
                                bdconexao                       = new QAction("Conexão com o BD", configura);
                                impressdoc                      = new QAction("Impressão de Documentos", configura);
                                
                                // Configuração das dicas de ferramenta
                                restricoes->setToolTip("Configura o acesso dos usuários ao sistema");
                                autobackup->setToolTip("Configura o backup automático do sistema");
                                bdconexao->setToolTip("Configura a conexão com o banco de dados");
                                
                                // Carregando a lista de itens
                                QList <QAction *> listconfig;
                                
                                listconfig << restricoes << autobackup << bdconexao << impressdoc;
                                

                                //Adicionando a lista de intens aos menus
                                configura->addActions(listconfig);
                                // Deixando a janela no centro da tela
                                QRect ct = geometry();
                                ct.moveCenter(QApplication::desktop()->availableGeometry().center());
                                setGeometry(ct);

                                }

                                WindowMain::~WindowMain()
                                {
                                delete mdiarea;
                                delete modmenubar;
                                }
                                @

                                The header has already posted above

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  anselmolsm
                                  wrote on 24 Jun 2011, 12:10 last edited by
                                  #17

                                  The current code handles QEvent::ToolTip in QMenuBar::event. For the menu items, you need to do the same QMenu::event().

                                  Just a side note: As you can see, Qt does not support this use case by default. Do a quick research in your system, how many applications show tooltips like those you want? IMHO menu and menu items should have self-explaining names. If the menu names are good enough, the tooltips are redundant. But that's just my opinion...

                                  Anselmo L. S. Melo (anselmolsm)

                                  1 Reply Last reply
                                  0

                                  11/17

                                  21 Jun 2011, 18:59

                                  • Login

                                  • Login or register to search.
                                  11 out of 17
                                  • First post
                                    11/17
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved