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
Code:
//MainWindow.cpp
void MainWindow::drawMap()
{
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...
Code:
//rectMap.cpp
#include "rectMap.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include <math.h>
#include <QGraphicsSceneHoverEvent>
#include <QStyleOptionGraphicsItem>
:color
(qrand
() % 256, qrand
() % 256, qrand
() % 256),
QGraphicsItem(parent
), m_isHovered
(false), m_mouseIsDown
(false) {
size = Size;
setAcceptHoverEvents(true);
}
{
m_isHovered = true;
}
{
m_isHovered = false;
}
{
m_mouseIsDown = true;
update();
}
{
m_mouseIsDown = false;
update();
}
QRectF RectMap
::boundingRect() const {
qreal adjust = 0.5;
return QRectF( 0 - adjust,
-262 - adjust, size
+ adjust,
523 + adjust
);
}
{
path.addRect(-100, -200, 20, 40);
return path;
}
{
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);
}
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
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.
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.
Re: Mouse hovering or press event not working?
rather graphicsView->setMouseTracking(1);
Re: Mouse hovering or press event not working?
How about
Code:
graphicsView->viewport()->setMouseTracking(true);
?
Re: Mouse hovering or press event not working?
seemed like a life-saver....but it did not work either :(
1 Attachment(s)
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.
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
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "testhoveritem.h"
MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
ui->m_graphicsView->setScene(m_scene);
m_scene->addItem(new TestHoverItem());
m_scene->addItem(item = new TestHoverItem());
item->setPos(50, 50);
}
MainWindow::~MainWindow()
{
delete ui;
}
testhoveritem.cpp
Code:
#include "testhoveritem.h"
#include <QPainter>
#include <QGraphicsSceneHoverEvent>
#include <QStyleOptionGraphicsItem>
#include <QPen>\
#include <QBrush>
, m_isHovered(false)
, m_mouseIsDown(false)
{
setAcceptHoverEvents(true);
m_rect
= QRect(0,
0,
100,
100);
}
{
m_isHovered = true;
}
{
m_isHovered = false;
}
{
m_mouseIsDown = true;
update();
}
{
m_mouseIsDown = false;
update();
}
{
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
Re: Mouse hovering or press event not working?
Your shape() implementation is incorrect. Remove (or correct) it and your code will start working.
Re: Mouse hovering or press event not working?
:D Wysota u rock.
Hovering and mouse press events working now.
Bundle of thanks. :)
Re: Mouse hovering or press event not working?