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. Constantly updating tooltip text

Constantly updating tooltip text

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 4.4k Views 3 Watching
  • 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.
  • sehnkimS Offline
    sehnkimS Offline
    sehnkim
    wrote on last edited by
    #1

    Hello all,

    I'd like to ask a question about the tooltip.

    I keep receiving position and orientation data from a server (python socket),
    and I want to display them using a tooltip.
    But I don't know how to keep updating the tooltip text.
    Note that the data is updating about 10 times per second.

    Thanks.

    the_T 1 Reply Last reply
    0
    • sehnkimS sehnkim

      Hello all,

      I'd like to ask a question about the tooltip.

      I keep receiving position and orientation data from a server (python socket),
      and I want to display them using a tooltip.
      But I don't know how to keep updating the tooltip text.
      Note that the data is updating about 10 times per second.

      Thanks.

      the_T Offline
      the_T Offline
      the_
      wrote on last edited by
      #2

      @sehnkim

      You could do that with signal/slot
      Every time you receive new data from your server you could fire a signal and in the connected slot you update the tooltip

      -- No support in PM --

      1 Reply Last reply
      0
      • sehnkimS Offline
        sehnkimS Offline
        sehnkim
        wrote on last edited by
        #3

        @the_

        Thank you for your reply.

        I've tried like this as SLOT

        ui->pushButton_add->setToolTip(QString::number(ncalls));

        but the tooltip shows only the latest value.
        I need to see the value changing continuously.

        Any advice?
        Thanks.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          A tooltip might not be the best tool for that. Why did you choose to use a tooltip ? How is your application supposed to show that changing value ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          sehnkimS 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            A tooltip might not be the best tool for that. Why did you choose to use a tooltip ? How is your application supposed to show that changing value ?

            sehnkimS Offline
            sehnkimS Offline
            sehnkim
            wrote on last edited by sehnkim
            #5

            @SGaist

            I want to show 'Tracker info + its position/orientation' whenever the user hovers the mouse over each item of the ListWidget. All icons are contained in a ListWidget.
            I was using the tooltip for showing the 'Tracker info', and I am trying to add its position/orientation to the info. That's why I am using a tooltip for the changing values.
            What approaches do you recommend?

            Here is a conceptual figure using the MS Paint.
            https://www.dropbox.com/s/iromipvip5680j4/listWidget.png?dl=0

            Thanks a lot. :)

            raven-worxR 1 Reply Last reply
            0
            • sehnkimS sehnkim

              @SGaist

              I want to show 'Tracker info + its position/orientation' whenever the user hovers the mouse over each item of the ListWidget. All icons are contained in a ListWidget.
              I was using the tooltip for showing the 'Tracker info', and I am trying to add its position/orientation to the info. That's why I am using a tooltip for the changing values.
              What approaches do you recommend?

              Here is a conceptual figure using the MS Paint.
              https://www.dropbox.com/s/iromipvip5680j4/listWidget.png?dl=0

              Thanks a lot. :)

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @sehnkim
              you can "simulate" a tooltip-like behavior, but more responsive.

              QLabel* toolTipWidget = new QLabel( this, Qt::ToolTip );
              ...
              toolTipWidget->setText( ... );
              toolTipWidget->move( pos );
              toolTipWidget->show();
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              sehnkimS 1 Reply Last reply
              1
              • raven-worxR raven-worx

                @sehnkim
                you can "simulate" a tooltip-like behavior, but more responsive.

                QLabel* toolTipWidget = new QLabel( this, Qt::ToolTip );
                ...
                toolTipWidget->setText( ... );
                toolTipWidget->move( pos );
                toolTipWidget->show();
                
                sehnkimS Offline
                sehnkimS Offline
                sehnkim
                wrote on last edited by
                #7

                @raven-worx

                Oh.. thanks a lot for your advice.
                If I follow your approach, how can I check if my mouse is over each item in a ListWidget? As I explained right above, I am using icons which are in a ListWidget.
                Here is a conceptual diagram by the MS Paint.
                https://www.dropbox.com/s/iromipvip5680j4/listWidget.png?dl=0
                Can I use eventFilter somehow?
                Thanks a lot.

                bool MainWindow::eventFilter(QObject *watched, QEvent *evt)
                {
                    if (watched == ui->pushButton)
                    {
                        if (evt->type() == QEvent::ToolTip)
                        {
                            QHelpEvent *helpEvent = static_cast<QHelpEvent *>(evt);
                            m_ToolTipPos = helpEvent->globalPos();
                        }
                        else if (evt->type() == QEvent::ToolTipChange)
                        {
                            if (ui->pushButton->underMouse() && !m_ToolTipPos.isNull())
                                QToolTip::showText(m_ToolTipPos, ui->pushButton->toolTip());
                        }
                    }
                
                    return QMainWindow::eventFilter(watched, evt);
                }
                
                raven-worxR 1 Reply Last reply
                0
                • sehnkimS sehnkim

                  @raven-worx

                  Oh.. thanks a lot for your advice.
                  If I follow your approach, how can I check if my mouse is over each item in a ListWidget? As I explained right above, I am using icons which are in a ListWidget.
                  Here is a conceptual diagram by the MS Paint.
                  https://www.dropbox.com/s/iromipvip5680j4/listWidget.png?dl=0
                  Can I use eventFilter somehow?
                  Thanks a lot.

                  bool MainWindow::eventFilter(QObject *watched, QEvent *evt)
                  {
                      if (watched == ui->pushButton)
                      {
                          if (evt->type() == QEvent::ToolTip)
                          {
                              QHelpEvent *helpEvent = static_cast<QHelpEvent *>(evt);
                              m_ToolTipPos = helpEvent->globalPos();
                          }
                          else if (evt->type() == QEvent::ToolTipChange)
                          {
                              if (ui->pushButton->underMouse() && !m_ToolTipPos.isNull())
                                  QToolTip::showText(m_ToolTipPos, ui->pushButton->toolTip());
                          }
                      }
                  
                      return QMainWindow::eventFilter(watched, evt);
                  }
                  
                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @sehnkim

                  listWidget->viewport()->setMouseTracking( true );
                  listWidget->viewport()->installEventFilter( this );
                  ...
                  bool eventFilter( QObject* watched, QEvent* event )
                  {
                      if( watched == listWidget->viewport() )
                      {
                           switch( event->type() )
                           {
                                  case QEvent::MouseMove:
                                  {
                                         QMouseEvent* me = static_cast<QMouseEvent*>( event );
                                         QModelIndex index = listWidget->indexAt( me->pos() );
                                         if( index.isValid() )
                                         {
                                                  // get the infos needed using the QModelIndex index
                                                  toolTipWidget->setText( ... );
                                                  toolTipWidget->move( me->globalPos() );
                                                  toolTipWidget->show();
                                         }
                                         else
                                         {
                                                  toolTipWidget->hide();
                                         }
                                  }
                           }
                      }
                      return BaseClass::eventFilter( watched, event );
                  }
                  

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  sehnkimS 1 Reply Last reply
                  6
                  • raven-worxR raven-worx

                    @sehnkim

                    listWidget->viewport()->setMouseTracking( true );
                    listWidget->viewport()->installEventFilter( this );
                    ...
                    bool eventFilter( QObject* watched, QEvent* event )
                    {
                        if( watched == listWidget->viewport() )
                        {
                             switch( event->type() )
                             {
                                    case QEvent::MouseMove:
                                    {
                                           QMouseEvent* me = static_cast<QMouseEvent*>( event );
                                           QModelIndex index = listWidget->indexAt( me->pos() );
                                           if( index.isValid() )
                                           {
                                                    // get the infos needed using the QModelIndex index
                                                    toolTipWidget->setText( ... );
                                                    toolTipWidget->move( me->globalPos() );
                                                    toolTipWidget->show();
                                           }
                                           else
                                           {
                                                    toolTipWidget->hide();
                                           }
                                    }
                             }
                        }
                        return BaseClass::eventFilter( watched, event );
                    }
                    
                    sehnkimS Offline
                    sehnkimS Offline
                    sehnkim
                    wrote on last edited by
                    #9

                    @raven-worx

                    I really appreciate your help and the example code which is very useful.
                    I have tested with your code, and worked pretty well mostly except one thing.

                    In my code, I have used
                    if (evt->type() == QEvent::ToolTipChange))
                    since I want to update the tooltip even though I don't move the mouse.
                    That is, I wanted to update the tooltip text without moving the mouse.

                    Actually, it worked pretty well with a PushButton, but it didn't work with a ListWidget since updating a tooltip for an item doesn't trigger the QEvent::ToolTipChange, but updating a tooltip for ListWidget itself triggered the event. :( Do you know how to trigger the event when updating a tooltip for an item in ListWidget?

                    In addition, here is the latest version based on your code. Would you mind proving me with any advice on how to update the label text without moving the mouse? I've tried with other events, but failed. :(

                    Thank you so much. :)

                    bool MainWindow::eventFilter( QObject* watched, QEvent* event )
                    {
                        if( watched == ui->listWidget->viewport() )
                        {
                            switch( event->type() )
                            {
                                case QEvent::MouseMove:
                                {
                                    QMouseEvent* me = static_cast<QMouseEvent*>( event );
                                    QModelIndex index = ui->listWidget->indexAt( me->pos() );
                                    if( index.isValid() ) {
                                        // get the infos needed using the QModelIndex index
                                        toolTipWidget->setText(QString::number(ncalls++));
                                        QPoint rePos = me->globalPos();
                                        rePos.setX(rePos.x() + 30);
                                        toolTipWidget->move( rePos );
                                        toolTipWidget->show();
                                    }
                                    else
                                        toolTipWidget->hide();
                                }
                            }
                        }
                        return QMainWindow::eventFilter( watched, event );
                    }
                    
                    raven-worxR 1 Reply Last reply
                    0
                    • sehnkimS sehnkim

                      @raven-worx

                      I really appreciate your help and the example code which is very useful.
                      I have tested with your code, and worked pretty well mostly except one thing.

                      In my code, I have used
                      if (evt->type() == QEvent::ToolTipChange))
                      since I want to update the tooltip even though I don't move the mouse.
                      That is, I wanted to update the tooltip text without moving the mouse.

                      Actually, it worked pretty well with a PushButton, but it didn't work with a ListWidget since updating a tooltip for an item doesn't trigger the QEvent::ToolTipChange, but updating a tooltip for ListWidget itself triggered the event. :( Do you know how to trigger the event when updating a tooltip for an item in ListWidget?

                      In addition, here is the latest version based on your code. Would you mind proving me with any advice on how to update the label text without moving the mouse? I've tried with other events, but failed. :(

                      Thank you so much. :)

                      bool MainWindow::eventFilter( QObject* watched, QEvent* event )
                      {
                          if( watched == ui->listWidget->viewport() )
                          {
                              switch( event->type() )
                              {
                                  case QEvent::MouseMove:
                                  {
                                      QMouseEvent* me = static_cast<QMouseEvent*>( event );
                                      QModelIndex index = ui->listWidget->indexAt( me->pos() );
                                      if( index.isValid() ) {
                                          // get the infos needed using the QModelIndex index
                                          toolTipWidget->setText(QString::number(ncalls++));
                                          QPoint rePos = me->globalPos();
                                          rePos.setX(rePos.x() + 30);
                                          toolTipWidget->move( rePos );
                                          toolTipWidget->show();
                                      }
                                      else
                                          toolTipWidget->hide();
                                  }
                              }
                          }
                          return QMainWindow::eventFilter( watched, event );
                      }
                      
                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by raven-worx
                      #10

                      @sehnkim
                      ok, depending on how you set the tooltip, this is a bit tricky, lets try it this way:
                      (Assuming you set the tooltip via the model directly or via QListWidgetItem::setToolTip())

                      class MyToolTipWidget : public QLabel
                      {
                          Q_OBJECT
                      
                      public:
                          MyToolTipWidget( QWidget* parent )
                              : QLabel( parent, Qt::ToolTip )
                          {
                          }
                        
                          void setCurrentIndex( const QModelIndex & index )
                          {
                              if( QAbstractItemModel* model = m_CurrentIndex.model() )
                                  model->disconnect(this);
                      
                              m_CurrentIndex = index;
                              this->updateToolTipText( m_CurrentIndex );
                      
                              if( QAbstractItemModel* model = m_CurrentIndex.model() )
                                  connect( model, SIGNAL(dataChanged(const QModelIndex &,const QModelIndex &)), this, SLOT(updateToolTipText(const QModelIndex &)) );
                          }
                          QModelIndex currentIndex() const { return m_CurrentIndex; }
                        
                      protected slots:
                          void updateToolTipText( const QModelIndex & index )
                          {
                              if( !m_CurretnIndex.isValid() || m_CurretnIndex != index )
                                  return;
                                  
                              // update of tooltip. Maybe m_CurrentIndex.data(Qt::ToolTipRole).toString() ??
                              this->setText( ... );
                          }
                        
                      protected:
                          virtual void hideEvent( QHideEvent* event )
                          {
                              this->setCurrentIndex( QModelIndex() );
                              QLabel::hideEvent( event );
                          }
                        
                      private:
                          QPersistentModelIndex   m_CurrentIndex;
                      };
                      

                      Just implement updateToolTipText() to your needs.
                      Now in the eventFilter you simply need to call setCurrentIndex() on the new tooltip widget and the rest should hopefully work out of the box.

                      Hope it works. Haven tested it, have just written it down straight from my head.

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      sehnkimS 1 Reply Last reply
                      1
                      • raven-worxR raven-worx

                        @sehnkim
                        ok, depending on how you set the tooltip, this is a bit tricky, lets try it this way:
                        (Assuming you set the tooltip via the model directly or via QListWidgetItem::setToolTip())

                        class MyToolTipWidget : public QLabel
                        {
                            Q_OBJECT
                        
                        public:
                            MyToolTipWidget( QWidget* parent )
                                : QLabel( parent, Qt::ToolTip )
                            {
                            }
                          
                            void setCurrentIndex( const QModelIndex & index )
                            {
                                if( QAbstractItemModel* model = m_CurrentIndex.model() )
                                    model->disconnect(this);
                        
                                m_CurrentIndex = index;
                                this->updateToolTipText( m_CurrentIndex );
                        
                                if( QAbstractItemModel* model = m_CurrentIndex.model() )
                                    connect( model, SIGNAL(dataChanged(const QModelIndex &,const QModelIndex &)), this, SLOT(updateToolTipText(const QModelIndex &)) );
                            }
                            QModelIndex currentIndex() const { return m_CurrentIndex; }
                          
                        protected slots:
                            void updateToolTipText( const QModelIndex & index )
                            {
                                if( !m_CurretnIndex.isValid() || m_CurretnIndex != index )
                                    return;
                                    
                                // update of tooltip. Maybe m_CurrentIndex.data(Qt::ToolTipRole).toString() ??
                                this->setText( ... );
                            }
                          
                        protected:
                            virtual void hideEvent( QHideEvent* event )
                            {
                                this->setCurrentIndex( QModelIndex() );
                                QLabel::hideEvent( event );
                            }
                          
                        private:
                            QPersistentModelIndex   m_CurrentIndex;
                        };
                        

                        Just implement updateToolTipText() to your needs.
                        Now in the eventFilter you simply need to call setCurrentIndex() on the new tooltip widget and the rest should hopefully work out of the box.

                        Hope it works. Haven tested it, have just written it down straight from my head.

                        sehnkimS Offline
                        sehnkimS Offline
                        sehnkim
                        wrote on last edited by
                        #11

                        @raven-worx

                        Thank you so much for your help.
                        I think the code would work for me. :)
                        Thanks.

                        1 Reply Last reply
                        0

                        • Login

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