PDA

View Full Version : Mouse hovering or press event not working?



qtzcute
13th July 2009, 06:18
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



//MainWindow.cpp
void MainWindow::drawMap()
{
QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(-360,-266,1000,532);
//scene->setItemIndexMethod(QGraphicsScene::NoIndex);
for (int i = 0; i < 50; ++i) {
RectMap *rectangle = new RectMap(50);
rectangle->setPos(xLocation,0);
xLocation = xLocation + 50 + 4 ;
scene->addItem(rectangle);
}
//graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
graphicsView->setScene(scene);
graphicsView->show();
}


and...



//rectMap.cpp
#include "rectMap.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include <math.h>
#include <QGraphicsSceneHoverEvent>
#include <QStyleOptionGraphicsItem>

RectMap::RectMap(int Size, QGraphicsItem *parent)
:color(qrand() % 256, qrand() % 256, qrand() % 256), QGraphicsItem(parent), m_isHovered(false), m_mouseIsDown(false)
{
size = Size;
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsMovable);
setAcceptHoverEvents(true);
}

void RectMap::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
{
m_isHovered = true;
QGraphicsItem::hoverEnterEvent(e);
}

void RectMap::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
{
m_isHovered = false;
QGraphicsItem::hoverLeaveEvent(e);
}

void RectMap::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
m_mouseIsDown = true;
QGraphicsItem::mousePressEvent(e);
update();
}

void RectMap::mouseReleaseEvent(QGraphicsSceneMouseEven t *e)
{
m_mouseIsDown = false;
QGraphicsItem::mouseReleaseEvent(e);
update();
}
QRectF RectMap::boundingRect() const
{
qreal adjust = 0.5;
return QRectF( 0 - adjust, -262 - adjust, size + adjust, 523 + adjust);
}

QPainterPath RectMap::shape() const
{
QPainterPath path;
path.addRect(-100, -200, 20, 40);
return path;
}


void RectMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPen pen;
bool isSelected = (option->state & QStyle::State_Selected) == QStyle::State_Selected;
pen.setColor(isSelected ? Qt::green : Qt::blue);
pen.setWidth(1);
pen.setStyle(m_mouseIsDown ? Qt::DashDotLine : Qt::DotLine);
painter->setPen(pen);
painter->setBrush(m_isHovered ? Qt::red : Qt::yellow);
painter->drawRect(0,-262,size,523);
}

qtzcute
13th July 2009, 06:23
I also followed following thread but still no success

http://www.qtforum.org/article/27811/i-get-a-mouse-button-reaction-but-nothing-on-hovering.html

wysota
13th July 2009, 09:03
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.

qtzcute
14th July 2009, 14:36
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.

qtzcute
14th July 2009, 14:38
rather graphicsView->setMouseTracking(1);

wysota
14th July 2009, 14:46
How about
graphicsView->viewport()->setMouseTracking(true);?

qtzcute
14th July 2009, 15:15
seemed like a life-saver....but it did not work either :(

qtzcute
14th July 2009, 15:19
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.

qtzcute
15th July 2009, 02:46
Just to save your time i thought to paste the finely working code of the example i attached in my last post

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "testhoveritem.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_scene = new QGraphicsScene(ui->m_graphicsView);
ui->m_graphicsView->setScene(m_scene);
QGraphicsItem *item;
m_scene->addItem(new TestHoverItem());
m_scene->addItem(item = new TestHoverItem());
item->setPos(50, 50);
}

MainWindow::~MainWindow()
{
delete ui;
}


testhoveritem.cpp

#include "testhoveritem.h"
#include <QPainter>
#include <QGraphicsSceneHoverEvent>
#include <QStyleOptionGraphicsItem>
#include <QPen>\
#include <QBrush>

TestHoverItem::TestHoverItem(QGraphicsItem * parent)
: QGraphicsItem(parent)
, m_isHovered(false)
, m_mouseIsDown(false)
{
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsMovable);
setAcceptHoverEvents(true);
m_rect = QRect(0, 0, 100, 100);
}

void TestHoverItem::hoverEnterEvent(QGraphicsSceneHover Event *e)
{
m_isHovered = true;
QGraphicsItem::hoverEnterEvent(e);
}

void TestHoverItem::hoverLeaveEvent(QGraphicsSceneHover Event *e)
{
m_isHovered = false;
QGraphicsItem::hoverLeaveEvent(e);
}

void TestHoverItem::mousePressEvent(QGraphicsSceneMouse Event *e)
{
m_mouseIsDown = true;
QGraphicsItem::mousePressEvent(e);
update();
}

void TestHoverItem::mouseReleaseEvent(QGraphicsSceneMou seEvent *e)
{
m_mouseIsDown = false;
QGraphicsItem::mouseReleaseEvent(e);
update();
}

void TestHoverItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QPen pen;
bool isSelected = (option->state & QStyle::State_Selected) == QStyle::State_Selected;
pen.setColor(isSelected ? Qt::green : Qt::blue);
pen.setWidth(5);
pen.setStyle(m_mouseIsDown ? Qt::DashDotLine : Qt::DotLine);
painter->setPen(pen);
painter->setBrush(m_isHovered ? Qt::red : Qt::yellow);
painter->drawRoundedRect(m_rect, 5, 5);
}

QRectF TestHoverItem::boundingRect() const
{
return QRectF(m_rect).adjusted(-1, -1, 1, 1);
}


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

wysota
15th July 2009, 09:26
Your shape() implementation is incorrect. Remove (or correct) it and your code will start working.

qtzcute
15th July 2009, 10:45
:D Wysota u rock.

Hovering and mouse press events working now.

Bundle of thanks. :)

wysota
15th July 2009, 10:53
No prob, as always.