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
It's al quit obvious in the screens I added.
The method setupGraphics() from class MainWindow looks like this:
void MainWindow::setupGraphics()
{
for (int i = 0; i < 256; ++i)
{
Graphics* item = new Graphics;
item->setPos(i*12,12);
scene->addItem(item);
}
m_ui.graphicsView->setScene(scene);
}
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);
}
To copy to clipboard, switch view to plain text mode
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 {
}
{
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);
}
{
if (!used)
{
used=true;
}
else
{
used=false;
}
update();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Thanx in advance
Bookmarks