PDA

View Full Version : Implementing paint()



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

guilugi
10th April 2007, 09:25
Well, the method is quite simple :)

You just have to use a variable to store your color, let's say bColor,
set by default with Qt::green.

1) In your mousePressEvent, you change this variable :


bColor = Qt::red;
update();


2) In your paint method


painter->setBrush(bColor);


I think that should work.

Wirloff
10th April 2007, 10:16
Thanx, update() was the methode that I was searching for. But now I have an other (weird) problem. I have added a 3 little zipfiles with 3 screenshots, which can show you the problem:

- When I click on a rectangle (I have 256 rectangels drawn next to each other on a GraphicsScene), not only thát rectangle is redrawn in red, but 2,5(!) rectangles are redrawn in red.

-After coloring some rectangles red, if I then scroll to the right, all the rectangles that weren't visible yet outside the screen, also have changed color :confused: It's al quit obvious in the screens I added.

The method setupGraphics() from class MainWindow looks like this:

void MainWindow::setupGraphics()
{
QGraphicsScene *scene = new QGraphicsScene();;
for (int i = 0; i < 256; ++i)
{
Graphics* item = new Graphics;
item->setPos(i*12,12);
scene->addItem(item);
}
m_ui.graphicsView->setScene(scene);
}

This draws the 256 rectangles for the first time....

Anyone who knows what I have to do so that only the rectangle which I have clicked on changes from color? Or what I'm doing wrong?? This is my code of the GraphicsItem again:( I have added a bool to control if an item has been clicked or not):


#include <QtGui>
#include <QPainter>
#include <iostream>
#include <QColor>

#include "Graphics.hpp"


bool used;

Graphics::Graphics(): color(qrand() % 256, qrand() % 256, qrand() % 256)
{
used=false;
}

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);
if (used)
{
painter->setBrush(Qt::red);
}
else
{
painter->setBrush(Qt::green);
}
painter->drawRect(0, 0, 10, 20);

}

void Graphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (!used)
{
used=true;
}
else
{
used=false;
}

update();

}

Thanx in advance

guilugi
10th April 2007, 10:29
Okay, I got it.

When you call update() without specifying a clip rect, it may cause adjacent items to be repainted as well...and that would explain your issues.
Read closely the docs about this method, it's well said :)

To avoid this, call update() with a specified rect (the current item rect) :


update( boundingRect() );