PDA

View Full Version : QGraphicsItem tooltip behavior



darksaga
22nd August 2007, 17:09
hi ppl,

I've got the following small prob:



#include <QtGui>

class MyGraphicsItem : public QGraphicsItem
{
public:
MyGraphicsItem(QGraphicsItem *parent = 0) : QGraphicsItem(parent), _width(40), _height(10){}
~MyGraphicsItem(void){qDebug("delBand");}

QRectF boundingRect() const{
return QRectF(0, 0, _width, _height);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0){
painter->setBrush(Qt::darkRed);
painter->setPen(Qt::black);
painter->drawRoundRect(0, 0, _width, _height);
}

private:
int _width;
int _height;
};


using:


setToolTip(QString("testToolTip"));

in the constructor, works perfectly to enable tooltips for the item

but using:


QString toolTip() const {
return QString("testToolTip");
}

does not work

why???:confused:

marcel
22nd August 2007, 17:12
because QGraphicsItem::tooltip is not virtual.
The view/scene will always call the base class version, which returns an empty string if you don't set any.

Regards

maverick_pol
22nd August 2007, 17:13
Hi,

This post should be removed. Sorry.

Maverick

maverick_pol
22nd August 2007, 17:15
Hi,

Ok...solved.
I have saved this post at the same time when the answer was given.
This post is not needed. I hope moderator or admin will remove it.


Maverick

darksaga
22nd August 2007, 17:24
because QGraphicsItem::tooltip is not virtual.
The view/scene will always call the base class version, which returns an empty string if you don't set any.

Regards

so there's no possibility to get the tooltip working without using setToolTip,

and to only to use a function like QString toolTip() const ?

marcel
22nd August 2007, 17:29
so there's no possibility to get the tooltip working without using setToolTip,

and to only to use a function like QString toolTip() const ?
No.

But if you do a little of extra work, you might be able to show a tooltip when you want it to, but manually.
Hint: override QGraphicsScene::helpEvent.

Regards

darksaga
22nd August 2007, 17:50
solved my prob the following way:

in constructor:


setAcceptsHoverEvents(true);




protected:
void hoverEnterEvent ( QGraphicsSceneHoverEvent * event ){
QString str = QString("testToolTip");
if (toolTip() != str){
setToolTip(str);
}
}


this does exactly, what I wanted,

but is a little long winded