Custom drawing is not that hard in general, but this tooltip problem will be pretty hard to solve.
First of all, you have to subclass the event() function for the widget you want the tip to appear.
In the event function you will look for a QHelpEvent, whose type is QEvent::ToolTip. This means that the widget is requested to show a tool tip.
The second step is to subclass QToolTip and override the paintEvent. In paintEvent you do all the drawing.
bool SomwWidget
::event(QEvent* e
) {
if(he
&& he
->type
() == QEvent::ToolTip) {
if(!mCustomToolTip)
mCustomToolTip = new CustomToolTip(this);
mCustomToolTip->show();
e->accept();
return true;
}
else
{
if(mCustomToolTip)
mCustomToolTip->hide();
}
}
bool SomwWidget::event(QEvent* e)
{
QHelpEvent *he = static_cast<QHelpEvent*>(e);
if(he && he->type() == QEvent::ToolTip)
{
if(!mCustomToolTip)
mCustomToolTip = new CustomToolTip(this);
mCustomToolTip->show();
e->accept();
return true;
}
else
{
if(mCustomToolTip)
mCustomToolTip->hide();
}
return QWidget::event(e);
}
To copy to clipboard, switch view to plain text mode
mCustomToolTip should be a member of your widget's class, which you initialize to NULL in the constructor.
For the paintEvent:
void CustomToolTip::paintEvent()
{
p.fillRect(rect(), Qt::transparent );
p.setBrush(Qt::red);
p.drawEllipse(rect());
}
void CustomToolTip::paintEvent()
{
QPainter p(this);
p.fillRect(rect(), Qt::transparent );
p.setBrush(Qt::red);
p.drawEllipse(rect());
}
To copy to clipboard, switch view to plain text mode
This draws a red ellipse on a transparent background.
It is just an example. You could customize the drawing further.
Regards
Bookmarks