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()
{
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();
}
//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();
}
To copy to clipboard, switch view to plain text mode
and...
//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);
}
//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(QGraphicsSceneMouseEvent *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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks