Hello
I have already search to the forum but i have not found something that it might help me (or at least, this is what i think)....
I have 2 sub-classes....The 1st is for the QGraphicsScene(graph_scene) and the 2nd is for the QGraphicsEllipseItem(piece).... From the graph_scene using the Q_FOREACH i am trying to make some changes into the objects of the sub-class piece....The objects of the sub-class piece has "Zvalue==2".... The problem is that when the program call the qgraphicsitem_cast it crash with the error "Segmentation fault"....I debug the program and i found that it crash on the line "Return Piece_Clicked;"
piece.h
{
public:
piece(graphics *ellipse,int);
int ReturnPieceClicked();
virtual ~piece();
enum { Type = UserType + 1 };
int type() const
{
return Type;
}
protected:
private:
QList<QGraphicsEllipseItem *> EllipseList;
int Piece_Clicked;
int Piece_atibue;
};
#endif // piece_H
class piece : public QGraphicsEllipseItem
{
public:
piece(graphics *ellipse,int);
int ReturnPieceClicked();
virtual ~piece();
enum { Type = UserType + 1 };
int type() const
{
return Type;
}
protected:
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
private:
QList<QGraphicsEllipseItem *> EllipseList;
int Piece_Clicked;
int Piece_atibue;
};
#endif // piece_H
To copy to clipboard, switch view to plain text mode
piece.cpp
.
.
.
{
if(Piece_Clicked!=0 || Piece_Clicked!=1) {
Piece_Clicked=0;
}
if(event->button()==Qt::LeftButton) {
setBrush
(QBrush(Qt
::darkMagenta));
qDebug() << "Piece Clicked" <<Piece_Clicked;
Piece_Clicked++;
if(Piece_Clicked!=1) {
Piece_Clicked=0;
qDebug() << "step 2";
}
}
}
int piece::ReturnPieceClicked()
{
[B][COLOR="red"]return Piece_Clicked;//this is the real place where the program is crashed....[/COLOR][/B]
}
.
.
.
void piece::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
if(Piece_Clicked!=0 || Piece_Clicked!=1) {
Piece_Clicked=0;
}
if(event->button()==Qt::LeftButton) {
setBrush(QBrush(Qt::darkMagenta));
qDebug() << "Piece Clicked" <<Piece_Clicked;
Piece_Clicked++;
if(Piece_Clicked!=1) {
Piece_Clicked=0;
qDebug() << "step 2";
}
}
}
int piece::ReturnPieceClicked()
{
[B][COLOR="red"]return Piece_Clicked;//this is the real place where the program is crashed....[/COLOR][/B]
}
To copy to clipboard, switch view to plain text mode
graph_scene.cpp
{
if(ClickCounter>2 || ClickCounter<0) {
MoveDone=0;
ClickCounter=0;
}
if(event->button()==Qt::LeftButton) {
ClickCounter++;
qDebug() << "left click is working";
if(ClickCounter==2) {
if(item->zValue()==1) {
qDebug() <<qgraphicsitem_cast<piece*>(item) << "koker";
if(qgraphicsitem_cast<piece*>(item)->ReturnPieceClicked()==1)
qDebug() <<"it works";
if(abs(item->pos().x())-abs(event->pos().x())==50)
qDebug() << "x ok";
if(abs(item->pos().y())-abs(event->pos().y())==25)
qDebug() << "y ok";
}
if(item->ReturnPieceClicked==1) {[B][COLOR="red"]//it crash here...with a first look[/COLOR][/B]
qDebug() << MoveDone << "before move";
if(item->zValue()==2 && MoveDone==0) {
qDebug() << event->scenePos() << "scene";
item->setPos(event->scenePos());
MoveDone++;
ClickCounter=0;
}
//}
}
}
}
qDebug() << "step 1";
qDebug() << ClickCounter << "counter";
qDebug() << MoveDone << "after move";
}
void graph_scene::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
if(ClickCounter>2 || ClickCounter<0) {
MoveDone=0;
ClickCounter=0;
}
QGraphicsScene::mousePressEvent(event);
if(event->button()==Qt::LeftButton) {
ClickCounter++;
qDebug() << "left click is working";
if(ClickCounter==2) {
Q_FOREACH(QGraphicsItem *item,items()) {
if(item->zValue()==1) {
qDebug() <<qgraphicsitem_cast<piece*>(item) << "koker";
if(qgraphicsitem_cast<piece*>(item)->ReturnPieceClicked()==1)
qDebug() <<"it works";
if(abs(item->pos().x())-abs(event->pos().x())==50)
qDebug() << "x ok";
if(abs(item->pos().y())-abs(event->pos().y())==25)
qDebug() << "y ok";
}
if(item->ReturnPieceClicked==1) {[B][COLOR="red"]//it crash here...with a first look[/COLOR][/B]
qDebug() << MoveDone << "before move";
if(item->zValue()==2 && MoveDone==0) {
qDebug() << event->scenePos() << "scene";
item->setPos(event->scenePos());
MoveDone++;
ClickCounter=0;
}
//}
}
}
}
qDebug() << "step 1";
qDebug() << ClickCounter << "counter";
qDebug() << MoveDone << "after move";
}
To copy to clipboard, switch view to plain text mode
Please can you help me?
Bookmarks