Results 1 to 10 of 10

Thread: Graphicsitem hover highlight

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Graphicsitem hover highlight

    Quote Originally Posted by Lykurg View Post
    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?

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Graphicsitem hover highlight

    Quote Originally Posted by wysota View Post
    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.
    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:
    Qt Code:
    1. class EditSceneItem: public QGraphicsTextItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. EditSceneItem(QGraphicsItem *parent = 0);
    7. virtual ~EditSceneItem();
    8. void setWord(Word *word);
    9.  
    10. protected:
    11. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    12.  
    13. private:
    14. Word *m_word;
    15. };
    16.  
    17. //==========
    18.  
    19. EditSceneItem::EditSceneItem(QGraphicsItem *parent) :
    20. {
    21. setAcceptHoverEvents(true);
    22. setTextInteractionFlags(Qt::LinksAccessibleByMouse);
    23. setZValue(50);
    24. }
    25.  
    26. EditSceneItem::~EditSceneItem()
    27. {
    28. }
    29.  
    30. void EditSceneItem::setWord(Word *word)
    31. {
    32. m_word = word;
    33. setPlainText(m_word->text());
    34. }
    35.  
    36. void EditSceneItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    37. {
    38. qWarning() << "paint!";
    39. painter->save();
    40. QRectF rect = option->rect;
    41. QPen pen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    42. if (option->state.testFlag(QStyle::State_HasFocus) )
    43. {
    44. pen.setColor(QColor(140, 140, 140));
    45. painter->setBrush(QColor(255, 255, 190));
    46. }
    47. else if (option->state.testFlag(QStyle::State_MouseOver))
    48. {
    49. pen.setColor(QColor(210, 210, 210));
    50. painter->setBrush(QColor(248, 248, 248));
    51. }
    52. else
    53. {// nonsense I know...
    54. pen.setColor(Qt::transparent);
    55. painter->setBrush(Qt::NoBrush);
    56. }
    57. painter->setPen(pen);
    58. rect.translate(-0.5, -0.5);
    59. painter->drawRoundedRect(rect, 5, 5);
    60. painter->restore();
    61.  
    62. QStyleOptionGraphicsItem *o = const_cast<QStyleOptionGraphicsItem*> (option);
    63. o->state &= ~QStyle::State_HasFocus;
    64. QGraphicsTextItem::paint(painter, o, widget);
    65. }
    To copy to clipboard, switch view to plain text mode 

    When hovering the item, "paint!" isn't shown up. adding
    Qt Code:
    1. void EditSceneItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
    2. {
    3. qWarning() << "hoverEnter";
    4. QGraphicsTextItem::hoverEnterEvent(event);
    5. }
    To copy to clipboard, switch view to plain text mode 
    prints me "hoverEnter" but no paint

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Graphicsitem hover highlight

    Ok, it's wired, look following example:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class myItem : public QGraphicsRectItem
    4. {
    5. public:
    6. myItem(QGraphicsItem *parent = 0) : QGraphicsRectItem(parent)
    7. {
    8. setAcceptHoverEvents(true);
    9. }
    10.  
    11. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
    12. {
    13. if (option->state.testFlag(QStyle::State_MouseOver))
    14. setBrush(QColor(0,230,230));
    15. else
    16. setBrush(QColor(230,230,230));
    17. QGraphicsRectItem::paint(painter, option, widget);
    18. }
    19. };
    20.  
    21. class myItem2 : public QGraphicsTextItem
    22. {
    23. public:
    24. myItem2(QGraphicsItem *parent = 0) : QGraphicsTextItem(parent)
    25. {
    26. setAcceptHoverEvents(true);
    27. }
    28.  
    29. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
    30. {
    31. painter->setBrush(QColor(230,230,230));
    32. if (option->state.testFlag(QStyle::State_MouseOver))
    33. painter->setBrush(QColor(0,230,230));
    34. painter->drawRect(option->rect);
    35. QGraphicsTextItem::paint(painter, option, widget);
    36. }
    37. };
    38.  
    39. class myItem3 : public QGraphicsSimpleTextItem
    40. {
    41. public:
    42. myItem3(QGraphicsItem *parent = 0) : QGraphicsSimpleTextItem(parent)
    43. {
    44. setAcceptHoverEvents(true);
    45. }
    46.  
    47. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
    48. {
    49. painter->setBrush(QColor(230,230,230));
    50. if (option->state.testFlag(QStyle::State_MouseOver))
    51. painter->setBrush(QColor(0,230,230));
    52. painter->drawRect(option->rect);
    53. QGraphicsSimpleTextItem::paint(painter, option, widget);
    54. }
    55. };
    56.  
    57. int main(int argc, char **argv)
    58. {
    59. QApplication app(argc, argv);
    60.  
    61. myItem it;
    62. it.setRect(0,0,30,30);
    63. scene.addItem(&it);
    64.  
    65. myItem2 it2;
    66. it2.setPlainText("test!");
    67. scene.addItem(&it2);
    68. it2.setPos(0,40);
    69.  
    70. myItem3 it3;
    71. it3.setText("test2!");
    72. scene.addItem(&it3);
    73. it3.setPos(0,80);
    74.  
    75. QGraphicsView view(&scene);
    76. view.show();
    77.  
    78. return app.exec();
    79. }
    To copy to clipboard, switch view to plain text mode 

    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:
    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...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Graphicsitem hover highlight

    Try redefining shape() of the item to boundingRect() and see what happens.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Graphicsitem hover highlight

    Hi, not quite sure if that is what you meant:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class myItem2 : public QGraphicsTextItem
    4. {
    5. public:
    6. myItem2(QGraphicsItem *parent = 0) : QGraphicsTextItem(parent)
    7. {
    8. setAcceptHoverEvents(true);
    9. }
    10.  
    11. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
    12. {
    13. painter->setBrush(QColor(230,230,230));
    14. if (option->state.testFlag(QStyle::State_MouseOver))
    15. painter->setBrush(QColor(0,230,230));
    16. painter->drawRect(option->rect);
    17. QGraphicsTextItem::paint(painter, option, widget);
    18. }
    19.  
    20. QPainterPath shape() const
    21. {
    22. path.addRect(boundingRect());
    23. qWarning() << boundingRect();
    24. return path;
    25. }
    26. };
    27.  
    28. int main(int argc, char **argv)
    29. {
    30. QApplication app(argc, argv);
    31.  
    32. myItem2 it2;
    33. it2.setPlainText("test");
    34. scene.addItem(&it2);
    35. QGraphicsView view(&scene);
    36. view.show();
    37.  
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

    When hover the item shape is called with the correct bounding rect. But the background don't change.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Graphicsitem hover highlight

    But is paint() called or not?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Graphicsitem hover highlight

    Quote Originally Posted by wysota View Post
    But is paint() called or not?
    no, it's not

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Graphicsitem hover highlight

    Quote Originally Posted by wysota View Post
    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.

    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
    Qt Code:
    1. protected:
    2. void hoverEnterEvent(QGraphicsSceneHoverEvent *) {update();}
    3. void hoverLeaveEvent(QGraphicsSceneHoverEvent *) {update();}
    To copy to clipboard, switch view to plain text mode 
    Thanks

Similar Threads

  1. Hover on mouse over while dragging
    By mooreaa in forum Qt Programming
    Replies: 3
    Last Post: 6th February 2010, 10:31
  2. QTextBrowser and Hover
    By yogeshgokul in forum Qt Programming
    Replies: 1
    Last Post: 5th January 2008, 11:18
  3. Hover and Highlight QTable and QTree Items
    By VireX in forum Qt Programming
    Replies: 41
    Last Post: 18th May 2007, 21:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.