Graphicsitem hover highlight
Hi, simple case:
custom QGraphicsTextItem inside a normal QGraphicsScene and I want to change the background when the mouse is over the item.
So: setAcceptHoverEvents(true) and the hoverEnterEvent() of the item is triggered. Fine, but unless I don't call update() explicit, the background color wont change. (paint with QStyle::State_MouseOver checking.)
And here's the problem. I thought, that the paint method is automatically called when hovering the item, so that you don't have to reimp hoverEnterEvent etc. If that's right, what option do I miss? (there is no mouse grabber item)
Thanks,
Lykurg
Re: Graphicsitem hover highlight
Quote:
Originally Posted by
Lykurg
Hi, simple case:
custom
QGraphicsTextItem inside a normal
QGraphicsScene and I want to change the background when the mouse is over the item.
So: setAcceptHoverEvents(true) and the hoverEnterEvent() of the item is triggered. Fine, but unless I don't call update() explicit, the background color wont change. (paint with QStyle::State_MouseOver checking.)
Do you call the base class implementation of the events?
Quote:
And here's the problem. I thought, that the paint method is automatically called when hovering the item, so that you don't have to reimp hoverEnterEvent etc. If that's right, what option do I miss? (there is no mouse grabber item)
That's right. If it doesn't work then probably your paint implementation is incorrect. Can we see it?
Re: Graphicsitem hover highlight
Quote:
Originally Posted by
wysota
Do you call the base class implementation of the events?
No, but that was just a test if the item receives the event. In the normal class I don't want to reimp the hoverEnterEvent.
Quote:
That's right. If it doesn't work then probably your paint implementation is incorrect. Can we see it?
The strange thing is, that the paint function is not called.
The content is:
Code:
{
Q_OBJECT
public:
virtual ~EditSceneItem();
void setWord(Word *word);
protected:
private:
Word *m_word;
};
//==========
{
setAcceptHoverEvents(true);
setTextInteractionFlags(Qt::LinksAccessibleByMouse);
setZValue(50);
}
EditSceneItem::~EditSceneItem()
{
}
void EditSceneItem::setWord(Word *word)
{
m_word = word;
setPlainText(m_word->text());
}
{
qWarning() << "paint!";
painter->save();
QPen pen
(Qt
::black,
1, Qt
::SolidLine, Qt
::RoundCap, Qt
::RoundJoin);
if (option
->state.
testFlag(QStyle::State_HasFocus) ) {
pen.
setColor(QColor(140,
140,
140));
painter
->setBrush
(QColor(255,
255,
190));
}
else if (option
->state.
testFlag(QStyle::State_MouseOver)) {
pen.
setColor(QColor(210,
210,
210));
painter
->setBrush
(QColor(248,
248,
248));
}
else
{// nonsense I know...
pen.setColor(Qt::transparent);
painter->setBrush(Qt::NoBrush);
}
painter->setPen(pen);
rect.translate(-0.5, -0.5);
painter->drawRoundedRect(rect, 5, 5);
painter->restore();
o
->state
&= ~
QStyle::State_HasFocus;
}
When hovering the item, "paint!" isn't shown up. adding
Code:
{
qWarning() << "hoverEnter";
}
prints me "hoverEnter" but no paint :confused:
Re: Graphicsitem hover highlight
Ok, it's wired, look following example:
Code:
#include <QtGui>
{
public:
{
setAcceptHoverEvents(true);
}
{
if (option
->state.
testFlag(QStyle::State_MouseOver)) else
setBrush
(QColor(230,
230,
230));
}
};
{
public:
{
setAcceptHoverEvents(true);
}
{
painter
->setBrush
(QColor(230,
230,
230));
if (option
->state.
testFlag(QStyle::State_MouseOver)) painter
->setBrush
(QColor(0,
230,
230));
painter->drawRect(option->rect);
}
};
{
public:
{
setAcceptHoverEvents(true);
}
{
painter
->setBrush
(QColor(230,
230,
230));
if (option
->state.
testFlag(QStyle::State_MouseOver)) painter
->setBrush
(QColor(0,
230,
230));
painter->drawRect(option->rect);
}
};
int main(int argc, char **argv)
{
myItem it;
it.setRect(0,0,30,30);
scene.addItem(&it);
myItem2 it2;
it2.setPlainText("test!");
scene.addItem(&it2);
it2.setPos(0,40);
myItem3 it3;
it3.setText("test2!");
scene.addItem(&it3);
it3.setPos(0,80);
view.show();
return app.exec();
}
myItem and myItem3 behaves like expected, myItem2 not. Even if you set various values for setTextInteractionFlags(). So I came to the conclusion it does not work for QGraphicsTextItem. The funny thing is that you can read in the docs to QGraphicsTextItem:
Quote:
Note: QGraphicsTextItem accepts hover events by default. You can change this with setAcceptHoverEvents().
Hm, I guess that the above behavier is not intended. So it seems to be a bug special for QGraphicsTextItem. If nobody speaks against, I'll report it the next days...
Re: Graphicsitem hover highlight
Try redefining shape() of the item to boundingRect() and see what happens.
Re: Graphicsitem hover highlight
Hi, not quite sure if that is what you meant:
Code:
#include <QtGui>
{
public:
{
setAcceptHoverEvents(true);
}
{
painter
->setBrush
(QColor(230,
230,
230));
if (option
->state.
testFlag(QStyle::State_MouseOver)) painter
->setBrush
(QColor(0,
230,
230));
painter->drawRect(option->rect);
}
{
path.addRect(boundingRect());
qWarning() << boundingRect();
return path;
}
};
int main(int argc, char **argv)
{
myItem2 it2;
it2.setPlainText("test");
scene.addItem(&it2);
view.show();
return app.exec();
}
When hover the item shape is called with the correct bounding rect. But the background don't change.
Re: Graphicsitem hover highlight
But is paint() called or not?
Re: Graphicsitem hover highlight
Quote:
Originally Posted by
wysota
But is paint() called or not?
no, it's not :(
Re: Graphicsitem hover highlight
I'd suggest using a debugger with a breakpoint on your path() implementation or on some of the hover events to see where the flow goes.
As a workaround I can suggest wrapping your text item into a rect item (with a parent-child relationship) and accepting hovers on the parent.
Re: Graphicsitem hover highlight
Quote:
Originally Posted by
wysota
I'd suggest using a debugger with a breakpoint on your path() implementation or on some of the hover events to see where the flow goes.
I'll look on it closer that week.
Quote:
As a workaround I can suggest wrapping your text item into a rect item (with a parent-child relationship) and accepting hovers on the parent.
Since the hover handlers get called, I simply added
Thanks