My blog has been moved to ariya.ofilabs.com.

Monday, December 19, 2005

Pulsating Effect for QToolTip

About ten months ago, I played around with the idea of pulsating effect in QToolTip. Basically, the background color is changed quickly to series of some colors. See this Flash animation. The actual effect is of course much smoother than what is shown in this Flash demo.

Unfortunately that trick does not work anymore with the new Qt 4 as you can't change QToolTip's palette. However, I am able to get the solution: find the QTipLabel and then apply the same crime.

void MyWindow::beat()
{
  static QPointer<QLabel> tipLabel;
  static int index = 0;

  if(!tipLabel)
    foreach(QWidget *widget, QApplication::allWidgets())
      if(widget->inherits("QTipLabel"))
        tipLabel = qobject_cast<QLabel*>(widget);
 
  if(tipLabel)
  {
    int k = (index>7) ? 14-index : index;
    index += (index<14) ? 1 : -14;
    
    QColor c = QColor( 255, 128+k*16, k*20 );
    QPalette pal = tipLabel->palette();
    pal.setColor( QPalette::Background, c );
    tipLabel->setPalette( pal );
  }
 
  QTimer::singleShot(tipLabel? 100 : 1000, this, SLOT(beat()));
}

This is again a dirty hack, but it works as long as your program does not create its own QTipLabel and QToolTip implementation won't be radically changed.

No comments: