سؤال

I'm having trouble allocating an object in my class. It feels like I'm doing it right but it's not letting me run. I've looked it up but I can't figure out what I'm doing wrong or different from all the others. I've commented out a bunch of stuff and yet it still won't run

rifle.h

#ifndef rifle_H
#define rifle_H

#include <QPainter>
#include <QGraphicsItem>
#include <QGraphicsScene>

#include <QTimer>
#include <QDialog>

class rifle : public QGraphicsItem
{
public:
    rifle();//(qreal dirx, qreal diry);
    //virtual ~rifle();
    void move();
protected:

private:
    //QPixmap shot;
    qreal m_Speed;
    qreal m_DirX;
    qreal m_DirY;
};

#endif // rifle_H

rifle.cpp

#include "rifle.h"

rifle::rifle() //qreal dirx, qreal diry
//    : m_Speed(5.0)
//    , m_DirX(dirx)
//    , m_DirY(diry)
{
    //shot.load(":/pic/rifle.png");
}

rifle::~rifle()
{

}


void rifle::move()
{
    setPos(x()+m_Speed*m_DirX, y()+m_Speed*m_DirY);
}

and here's where I'm doing the code

void Dialog::fire()
{
    qreal dirx = ui->graphicsView->m_FireTarget.x()-200.0;
    qreal diry = ui->graphicsView->m_FireTarget.y()-200.0;

    qreal length = sqrt(dirx*dirx+diry*diry);
    if (length!=0)
    {
        // normalized direction vector
        qreal invLength= 1.0/length;
        dirx *= invLength;
        diry *= invLength;

        // creating an angle perturbation of +/- 3°
        qreal alphaPerturbation = static_cast<qreal>(qrand()%6-3) * M_PI / 180.0;
        qreal xPerturbation = cos(alphaPerturbation);
        qreal yPerturbation = sin(alphaPerturbation);
        // cos(a+b)=...
        dirx = dirx*xPerturbation - diry*yPerturbation;
        // sin(a+b)=...
        diry = diry*xPerturbation + dirx*yPerturbation;

        /*rifle**/ circle = new rifle(dirx, diry);
        scene->addItem(circle);
    }
}
هل كانت مفيدة؟

المحلول

http://qt-project.org/doc/qt-4.8/qgraphicsitem.html

To write your own graphics item, you first create a subclass of QGraphicsItem, and then start by implementing its two pure virtual public functions: boundingRect(), which returns an estimate of the area painted by the item, and paint(), which implements the actual painting.

You must override these functions in rifle as they are pure virtual functions in the base class.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top