Wirloff
10th April 2007, 09:19
Hi, I have a class named "Graphics" which is derived from "QGraphicsItem" and represents a small green rectangle:
#include <QtGui>
#include <QPainter>
#include <iostream>
#include <QColor>
#include "Graphics.hpp"
Graphics::Graphics(): color(qrand() % 256, qrand() % 256, qrand() % 256)
{
setCursor(Qt::OpenHandCursor);
}
QRectF Graphics::boundingRect() const
{
return QRectF(0, 0, 25, 25);
}
void Graphics::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::green);
painter->drawRect(0, 0, 10, 20);
}
void Graphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
}
But now, when clicked on a rectangle, I want it's color to change to red. But I have read that there is no such thing as a "repaint" methode. Anoyne who can help me, and tell me how to get the clicked rectangle red? I have no idea what to do within the mouseEvent
#include <QtGui>
#include <QPainter>
#include <iostream>
#include <QColor>
#include "Graphics.hpp"
Graphics::Graphics(): color(qrand() % 256, qrand() % 256, qrand() % 256)
{
setCursor(Qt::OpenHandCursor);
}
QRectF Graphics::boundingRect() const
{
return QRectF(0, 0, 25, 25);
}
void Graphics::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::green);
painter->drawRect(0, 0, 10, 20);
}
void Graphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
}
But now, when clicked on a rectangle, I want it's color to change to red. But I have read that there is no such thing as a "repaint" methode. Anoyne who can help me, and tell me how to get the clicked rectangle red? I have no idea what to do within the mouseEvent