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
){
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
testhoveritem.cpp
#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);
}
#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(QGraphicsSceneHoverEvent *e)
{
m_isHovered = true;
QGraphicsItem::hoverEnterEvent(e);
}
void TestHoverItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
{
m_isHovered = false;
QGraphicsItem::hoverLeaveEvent(e);
}
void TestHoverItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
m_mouseIsDown = true;
QGraphicsItem::mousePressEvent(e);
update();
}
void TestHoverItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *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);
}
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.
Bookmarks