Results 1 to 12 of 12

Thread: Mouse hovering or press event not working?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Mouse hovering or press event not working?

    How about
    Qt Code:
    1. graphicsView->viewport()->setMouseTracking(true);
    To copy to clipboard, switch view to plain text mode 
    ?
    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.


  2. #2
    Join Date
    Jun 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8

    Default Re: Mouse hovering or press event not working?

    seemed like a life-saver....but it did not work either

  3. #3
    Join Date
    Jun 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8

    Default Re: Mouse hovering or press event not working?

    I used the attached example program where the hovering and mouse press event are working just fine. I copied most of that code but still i am missing something.
    Attached Files Attached Files

  4. #4
    Join Date
    Jun 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8

    Default Re: Mouse hovering or press event not working?

    Just to save your time i thought to paste the finely working code of the example i attached in my last post

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "testhoveritem.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent), ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. m_scene = new QGraphicsScene(ui->m_graphicsView);
    10. ui->m_graphicsView->setScene(m_scene);
    11. m_scene->addItem(new TestHoverItem());
    12. m_scene->addItem(item = new TestHoverItem());
    13. item->setPos(50, 50);
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    To copy to clipboard, switch view to plain text mode 

    testhoveritem.cpp
    Qt Code:
    1. #include "testhoveritem.h"
    2. #include <QPainter>
    3. #include <QGraphicsSceneHoverEvent>
    4. #include <QStyleOptionGraphicsItem>
    5. #include <QPen>\
    6. #include <QBrush>
    7.  
    8. TestHoverItem::TestHoverItem(QGraphicsItem * parent)
    9. : QGraphicsItem(parent)
    10. , m_isHovered(false)
    11. , m_mouseIsDown(false)
    12. {
    13. setFlag(QGraphicsItem::ItemIsSelectable);
    14. setFlag(QGraphicsItem::ItemIsMovable);
    15. setAcceptHoverEvents(true);
    16. m_rect = QRect(0, 0, 100, 100);
    17. }
    18.  
    19. void TestHoverItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
    20. {
    21. m_isHovered = true;
    22. QGraphicsItem::hoverEnterEvent(e);
    23. }
    24.  
    25. void TestHoverItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
    26. {
    27. m_isHovered = false;
    28. QGraphicsItem::hoverLeaveEvent(e);
    29. }
    30.  
    31. void TestHoverItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
    32. {
    33. m_mouseIsDown = true;
    34. QGraphicsItem::mousePressEvent(e);
    35. update();
    36. }
    37.  
    38. void TestHoverItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    39. {
    40. m_mouseIsDown = false;
    41. QGraphicsItem::mouseReleaseEvent(e);
    42. update();
    43. }
    44.  
    45. void TestHoverItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    46. {
    47. QPen pen;
    48. bool isSelected = (option->state & QStyle::State_Selected) == QStyle::State_Selected;
    49. pen.setColor(isSelected ? Qt::green : Qt::blue);
    50. pen.setWidth(5);
    51. pen.setStyle(m_mouseIsDown ? Qt::DashDotLine : Qt::DotLine);
    52. painter->setPen(pen);
    53. painter->setBrush(m_isHovered ? Qt::red : Qt::yellow);
    54. painter->drawRoundedRect(m_rect, 5, 5);
    55. }
    56.  
    57. QRectF TestHoverItem::boundingRect() const
    58. {
    59. return QRectF(m_rect).adjusted(-1, -1, 1, 1);
    60. }
    To copy to clipboard, switch view to plain text mode 

    I reused the above code but mine is not working. My code is given in the post dated 13th July 2009 10:18 on this same thread.

    Still figuring out what the problem is.

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

    Default Re: Mouse hovering or press event not working?

    Your shape() implementation is incorrect. Remove (or correct) it and your code will start working.
    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. The following user says thank you to wysota for this useful post:

    qtzcute (15th July 2009)

  7. #6
    Join Date
    Jun 2009
    Posts
    38
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8

    Thumbs up Re: Mouse hovering or press event not working?

    Wysota u rock.

    Hovering and mouse press events working now.

    Bundle of thanks.

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

    Default Re: Mouse hovering or press event not working?

    No prob, as always.
    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.


Similar Threads

  1. Mouse press event in a QGraphicsScene
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 10:28
  2. Mouse press event detection
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 13:08
  3. Checking for key press on mouse event
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 24th January 2009, 18:18
  4. Replies: 1
    Last Post: 24th October 2007, 18:34
  5. Draw QtCanvasElipse on mouse press event position
    By YuriyRusinov in forum Newbie
    Replies: 1
    Last Post: 31st May 2006, 11:57

Tags for this Thread

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.