Results 1 to 12 of 12

Thread: Mouse hovering or press event not working?

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

    Red face Mouse hovering or press event not working?

    I am using following code to display a range of rectangles that can be clicked. Once clicked or hovered, first it should be highlighted and secondly some operation may be performed as well. But my program could not even highlight when i hover or press mouse button.

    Following is the bits of code that may have the problem. Its a little lengthy but very easy code. Please review it and tell me if you have some suggestions for me...Thanks a lot

    Qt Code:
    1. //MainWindow.cpp
    2. void MainWindow::drawMap()
    3. {
    4. scene->setSceneRect(-360,-266,1000,532);
    5. //scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    6. for (int i = 0; i < 50; ++i) {
    7. RectMap *rectangle = new RectMap(50);
    8. rectangle->setPos(xLocation,0);
    9. xLocation = xLocation + 50 + 4 ;
    10. scene->addItem(rectangle);
    11. }
    12. //graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
    13. graphicsView->setScene(scene);
    14. graphicsView->show();
    15. }
    To copy to clipboard, switch view to plain text mode 

    and...

    Qt Code:
    1. //rectMap.cpp
    2. #include "rectMap.h"
    3. #include <QGraphicsScene>
    4. #include <QPainter>
    5. #include <QStyleOption>
    6. #include <math.h>
    7. #include <QGraphicsSceneHoverEvent>
    8. #include <QStyleOptionGraphicsItem>
    9.  
    10. RectMap::RectMap(int Size, QGraphicsItem *parent)
    11. :color(qrand() % 256, qrand() % 256, qrand() % 256), QGraphicsItem(parent), m_isHovered(false), m_mouseIsDown(false)
    12. {
    13. size = Size;
    14. setFlag(QGraphicsItem::ItemIsSelectable);
    15. setFlag(QGraphicsItem::ItemIsMovable);
    16. setAcceptHoverEvents(true);
    17. }
    18.  
    19. void RectMap::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
    20. {
    21. m_isHovered = true;
    22. QGraphicsItem::hoverEnterEvent(e);
    23. }
    24.  
    25. void RectMap::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
    26. {
    27. m_isHovered = false;
    28. QGraphicsItem::hoverLeaveEvent(e);
    29. }
    30.  
    31. void RectMap::mousePressEvent(QGraphicsSceneMouseEvent *e)
    32. {
    33. m_mouseIsDown = true;
    34. QGraphicsItem::mousePressEvent(e);
    35. update();
    36. }
    37.  
    38. void RectMap::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
    39. {
    40. m_mouseIsDown = false;
    41. QGraphicsItem::mouseReleaseEvent(e);
    42. update();
    43. }
    44. QRectF RectMap::boundingRect() const
    45. {
    46. qreal adjust = 0.5;
    47. return QRectF( 0 - adjust, -262 - adjust, size + adjust, 523 + adjust);
    48. }
    49.  
    50. QPainterPath RectMap::shape() const
    51. {
    52. path.addRect(-100, -200, 20, 40);
    53. return path;
    54. }
    55.  
    56.  
    57. void RectMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    58. {
    59. QPen pen;
    60. bool isSelected = (option->state & QStyle::State_Selected) == QStyle::State_Selected;
    61. pen.setColor(isSelected ? Qt::green : Qt::blue);
    62. pen.setWidth(1);
    63. pen.setStyle(m_mouseIsDown ? Qt::DashDotLine : Qt::DotLine);
    64. painter->setPen(pen);
    65. painter->setBrush(m_isHovered ? Qt::red : Qt::yellow);
    66. painter->drawRect(0,-262,size,523);
    67. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Mouse hovering or press event not working?

    I also followed following thread but still no success

    http://www.qtforum.org/article/27811...-hovering.html

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

    Default Re: Mouse hovering or press event not working?

    Did you enable mouse tracking?

    Ignore your hover events (of course you still need to accept them), you don't need to reimplement them. QStyleOption has a QStyle::State_MouseOver that will tell you if the item is being hovered.

    By the way, you do realize you are duplicating much of the QGraphicsItemRect class? It would be easier if you just used it instead of QGraphicsItem.
    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.


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

    Default Re: Mouse hovering or press event not working?

    No i did not. I just did it by setting graphicsView->setMouseTracking(0); but i am still not able to getting a changed border color due to mouse click or rectangle color change due to hovering.

    And as you aske me to, this time i did not reimplemented the hovering and mouse click event.

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

    Default Re: Mouse hovering or press event not working?

    rather graphicsView->setMouseTracking(1);

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


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

    Default Re: Mouse hovering or press event not working?

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

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

    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

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

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


  11. The following user says thank you to wysota for this useful post:

    qtzcute (15th July 2009)

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

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

    Wysota u rock.

    Hovering and mouse press events working now.

    Bundle of thanks.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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, 11:28
  2. Mouse press event detection
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 14:08
  3. Checking for key press on mouse event
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 24th January 2009, 19:18
  4. Replies: 1
    Last Post: 24th October 2007, 19:34
  5. Draw QtCanvasElipse on mouse press event position
    By YuriyRusinov in forum Newbie
    Replies: 1
    Last Post: 31st May 2006, 12: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.