2017-11-27 10 views
0

私は、カーオブジェクトがある点を表示する単純なプログラムを作成しています。この点が後で可能なようにタイマを使用しているところを更新します移動する。オブジェクトを入力として使用してQt Creatorを描画しようとしています

現在のところ、私のコードは(0、0)にポイントを表示しているだけなので、私はその理由を理解できません。

マイmainwindow.cpp:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include <iostream> 
#include "twovector.h" 
#include "car.h" 
#include "vehicle.h" 
#include <vector> 
#include <QTimer> 
#include <QStandardItem> 
#include <QPainter> 

using namespace std; 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 


    fCar = new Car(TwoVector(150, 2), 4); 


    QTimer *timer = new QTimer(this); 
    connect(timer, SIGNAL(timeout()), this, SLOT(paintEvent())); 
    // connect(timer, SIGNAL(timeout()), this, SLOT(Drive())); 
    timer->start(10); 


} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::Drive(){ 
    fCar->Drive(fCar->GetVelocity()); 
} 

void MainWindow::paintEvent(QPaintEvent *){ 

    QPainter painter(this); 
    QPen DotPen(Qt::red); 
    DotPen.setWidth(10); 

    painter.setPen(DotPen); 
    //painter.drawPoint(0, 0); 

    painter.drawPoint((fCar->GetPosition().GetRadius())*(cos(fCar->GetPosition().GetAngle())) 
         , (fCar->GetPosition().GetRadius())*(sin(fCar->GetPosition().GetAngle()))); 
     //Draw the dot, where the centre of the window is (0,0) 
} 

メインウィンドウヘッダーファイル:(車クラスから継承する)

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include "car.h" 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

    void Drive(); 

    void paintEvent(QPaintEvent *); 

private: 
    Ui::MainWindow *ui; 

    Car *fCar; 
}; 

#endif // MAINWINDOW_H 

vehicle.ccp

#include "vehicle.h" 
#include "twovector.h" 
#include <iostream> 
using namespace std; 

Vehicle::Vehicle(){ 
} 

Vehicle::Vehicle(TwoVector position, double velocity){ 
    fPosition = position; 
    fVelocity = velocity; 
} 

Vehicle::~Vehicle(){ 
} 

void Vehicle::SetValue(string ValueName, double Value) { 
    if(ValueName.compare("Radius") == 0) 
     fPosition.SetRadius(Value); 
    else{ 
     if(ValueName.compare("Angle") == 0) 
      fPosition.SetAngle(Value); 
     else if(ValueName.compare("Velocity") == 0) 
      fVelocity = Value; 
     else 
      cerr << "Unknown field entered: " << ValueName << endl; 
    } 

} 

void Vehicle::Drive(int velocity){ 
    fPosition.SetAngle(fPosition.GetAngle() + (velocity)/(fPosition.GetRadius())); 
} 

vehicle.h :

#ifndef VEHICLE_H 
#define VEHICLE_H 
#include "twovector.h" 
#include <iostream> 
using namespace std; 

class Vehicle 
{ 
public: 
    Vehicle(); 

    Vehicle(TwoVector position, double velocity); 

    ~Vehicle(); 

    inline TwoVector GetPosition() {return fPosition;} 

    inline double GetVelocity() {return fVelocity;} 

    inline void SetPosition(TwoVector position) {fPosition = position;} 

    void SetValue(string ValueName, double Value); 

    void Drive(int velocity); 

private: 

    TwoVector fPosition; 
    double fVelocity; 
}; 

#endif // VEHICLE_H 

Twovector.ccp:

#include "twovector.h" 

TwoVector::TwoVector(){ 
    fRadius = 0; 
    fTheta = 0; 
} 

TwoVector::TwoVector(double radius, double theta){ 
    fRadius = radius; 
    fTheta = theta; 
} 

TwoVector::~TwoVector(){ 
} 

TwoVector TwoVector::operator +=(TwoVector position1){ 
    TwoVector position2; 
     //Creates a new object which is given a position 
    position2.fRadius = sqrt(((fRadius)*(fRadius))+((position1.fRadius)*(position1.fRadius)) 
          + 2*((position1.fRadius)*(fRadius))*cos((position1.fTheta)-(fTheta))); 
    position2.fTheta = fTheta + atan2((position1.fRadius)*(sin((position1.fTheta)-(fTheta))), 
           fRadius + (position1.fRadius)*(cos((position1.fTheta)-fTheta))); 
    return(position2); 
    //New position returned 

} 

TwoVector.h:

#ifndef TWOVECTOR_H 
#define TWOVECTOR_H 
#include <math.h> 


class TwoVector { 
public: 
    TwoVector(); 

    TwoVector(double radius, double theta); 

    ~TwoVector(); 

    inline double GetX() {return fRadius*cos(fTheta);} 
    inline double GetY() {return fRadius*sin(fTheta);} 
    inline double GetRadius() const {return fRadius;} 
    inline double GetAngle() const {return fTheta;} 
     //Accessor functions, these simply return the value of the coordinates 


    inline void SetRadius(double radius) {fRadius = radius;} 
    inline void SetAngle(double theta) {fTheta = theta;} 
    inline void SetRadiusAndAngle(double radius, double theta) { 
     fRadius = radius, fTheta = theta;} 
     //Mutator function to change the position 

    TwoVector operator += (TwoVector); 
     //Operator overloading so that vectors can be added 


private: 

    double fRadius; 
    double fTheta; 

}; 

#endif // TWOVECTOR_H 

car.h:

#ifndef CAR_H 
#define CAR_H 
#include "twovector.h" 
#include "vehicle.h" 
#include <iostream> 
using namespace std; 

class Car: public Vehicle { 
public: 
    Car(); 

    Car(TwoVector position, double velocity); 

    ~Car(); 

private: 

    TwoVector fPositioncar; 
    double fVelocitycar; 

}; 

#endif // CAR_H 

car.cpp:

#include "car.h" 

Car::Car(){ 
} 

Car::Car(TwoVector position, double velocity){ 
    fPositioncar = position; 
    fVelocitycar = velocity; 
} 

Car::~Car(){ 
} 

すべてのヘルプ感謝されます!

答えて

0

が直接paintEventメソッドを呼び出さないでください、あなたはupdate()方法でそれを行う必要がありますし、あなたの場合のように、あなたがポジションを更新する、以下の変更を行うことをお勧めします:

[...] 

    QTimer *timer = new QTimer(this); 
    //connect(timer, SIGNAL(timeout()), this, SLOT(paintEvent())); 
    connect(timer, SIGNAL(timeout()), this, SLOT(Drive())); 
    timer->start(10); 
[...] 

void MainWindow::Drive(){ 
    fCar->Drive(fCar->GetVelocity()); 
    update(); 
} 

[...] 

別の問題あなたが親のコンストラクタを呼び出していないということです。

car.cpp

#include "car.h" 

Car::Car():Vehicle(){ 
} 

Car::Car(TwoVector position, double velocity):Vehicle(position, velocity){ 
    fPositioncar = position; 
    fVelocitycar = velocity; 
} 

Car::~Car(){ 
} 
修正を加えた完全なプロジェクトをすることができ

vehicle.cpp

[...] 
void Vehicle::Drive(double velocity){ 
    fPosition.SetAngle(fPosition.GetAngle() + (velocity)/(fPosition.GetRadius())); 
} 

:も速度が一方のドライブを整数のために設定された上で、あなたはそれを変更する必要があり、double型の値であり、次のように表示されますlink

+0

OKああ、そう更新機能は、それがメインループに戻ったときにペイントイベントを呼び出すために、プログラムを教えてくれるか?私はそれを得ると思いますが、ポイントはまだ動いていないか、またはオブジェクトの位置から始めることさえありませんか? –

+0

Carクラスとは何ですか?プロジェクト全体を共有してあなたを助けることができます。 – eyllanesc

+0

車クラスには、ちょうど、コンストラクタとデストラクタを持っており、車両クラスから継承する - 現時点では無意味なのは、その種が、そのちょうど私が車クラスから継承するオブジェクトの種類を追加したときに、後でそれを構造化するためです。主な質問に追加します。 –