HI thanx for the replay.
It got me thinking and solved it in a way :).
As far did I only added singles that can be emitted white parameters.
is there some ware a list of mouse events that i can use for a QGraphicsItem.
Like :
right mouse button pressed.
moved into QGraphicsItem.
moved out QGraphicsItem.
so this is my square.cpp now
@#include "square.h"
square::square(int x, int y, QGraphicsObject *parent) :
QGraphicsObject(parent)
{
Pressed = false;
setFlag(ItemIsSelectable);
X = x;
Y = y;
}
QRectF square::boundingRect() const
{
return QRectF(X,Y,30,30);
}
void square::mousePressEvent(QGraphicsSceneMouseEvent *ev)
{
Pressed = true;
update();
QGraphicsItem::mousePressEvent(ev);
emit Mouse_Pressed(X,Y);
}
void square::mouseReleaseEvent(QGraphicsSceneMouseEvent *ev)
{
Pressed = false;
update();
QGraphicsItem::mousePressEvent(ev);
emit Mouse_Released(X,Y);
}
void square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec = boundingRect();
QBrush brush(Qt::blue);
if(Pressed)
{
brush.setColor(Qt::red);
}
else
{
brush.setColor(Qt::blue);
}
painter->fillRect(rec,brush);
painter->drawRect(rec);
}@
and mij Dialog.cpp looks like.
@#include "dialog.h"
#include "ui_dialog.h"
#include "square.h"
#include <QLabel>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
LineCounter = 0;
for (int i = 0; i < 25; ++i) {
if (i==0){
Hoog = i*30;
}
else {
Hoog = Hoog +30;
}
if (LineCounter == 5){
links = links+30;
Hoog = 0;
LineCounter = 1;
}
else{
LineCounter = LineCounter +1;
}
sq = new square(links,Hoog);
scene->addItem(sq);
connect(sq,SIGNAL(Mouse_Pressed(int,int)),this,SLOT(Mouse_Pressed(int,int)));
connect(sq,SIGNAL(Mouse_Released(int,int)),this,SLOT(Mouse_Released(int,int)));
}
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::Mouse_Pressed(int x,int y)
{
ui->label->setText(QString("Pressed X= %1, Y= %2 ").arg(x).arg(y));
}
void Dialog::Mouse_Released(int x, int y)
{
ui->label->setText(QString("Released X= %1, Y= %2 ").arg(x).arg(y));
}@