Класс ball
:
ball.h:
#ifndef BALL_H
#define BALL_H
#include <QGraphicsItem>
#include <iostream>
class ball
{
public:
ball(int x, int y,int R);
void setPos(int newX, int newY);
void setSpeed(int newSpeed);
QPoint getPos();
int getSpeed();
QGraphicsEllipseItem* getBall();
private:
int x;
int y;
int R;
int speed;
QGraphicsItem* ballObj;
};
#endif // BALL_H
ball.cpp:
#include "ball.h"
ball::ball(int x, int y,int R)
{
this->x=x+10;
this->y=y;
this->R=R;
ballObj = new QGraphicsEllipseItem(x,y,R,R);
}
void ball::setPos(int newX, int newY)
{
this->x=newX;
this->y=newY;
}
void ball::setSpeed(int newSpeed)
{
this->speed=newSpeed;
}
QGraphicsEllipseItem* ball::getBall()
{
return this->ballObj;
}
QPoint ball::getPos()
{
return QPoint(this->x,this->y);
}
int ball::getSpeed()
{
return this->speed;
}
Создание объекта:
ballObj=new ball(0,this->height(),10);
gameBoard->scene()->addItem( ballObj->getBall());
Конструктор GameBoard:
GameBoard::GameBoard(QWidget *parent)
{
gameBoard = new QGraphicsView();
mainLayout = new QVBoxLayout();
gameBoard->setParent(parent);
gameBoard->setMinimumSize(parent->width(),parent->height());
gameBoard->setMouseTracking(true);
gameBoard->grabMouse();
boardPen.setColor(Qt::black);
boardlBrush.setStyle(Qt::SolidPattern);
scene=new QGraphicsScene();
scene->addEllipse(0, 0, boardlWidth,boardHeigh, boardPen, boardlBrush);
ballObj=new ball(0,this->height(),10);
gameBoard->scene()->addItem( ballObj->getBall()); // Строка 20
gameBoard->setScene(scene); // 21
}
Консруктор - отрабатывает нормально - смотрел под отладчиком:
- скриншот следующего шага после срабатывания точки останова
, но при попытке добавить на сцену -получаю SegmentationFault - см скриншот: