尝试在 Qt Creator 中绘制,使用对象作为输入
Posted
技术标签:
【中文标题】尝试在 Qt Creator 中绘制,使用对象作为输入【英文标题】:Trying to draw in Qt Creator, using an object as an input 【发布时间】:2017-11-27 21:30:35 【问题描述】:我只是在创建一个简单的程序,它显示汽车对象所在的点,并使用计时器更新该点的位置,以便稍后它可以移动。
我的代码目前只显示 (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()));
车辆.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
双向量.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
汽车.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
汽车.cpp:
#include "car.h"
Car::Car()
Car::Car(TwoVector position, double velocity)
fPositioncar = position;
fVelocitycar = velocity;
Car::~Car()
任何帮助将不胜感激!
【问题讨论】:
【参考方案1】:不要直接调用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()
velocity 也是 double 类型的值,另一方面 Drive 配置为整数,您必须将其更改为:
vehicle.cpp
[...]
void Vehicle::Drive(double velocity)
fPosition.SetAngle(fPosition.GetAngle() + (velocity)/(fPosition.GetRadius()));
修改后的完整工程见以下link。
【讨论】:
嗯好的,那么update函数会告诉程序在返回主循环时调用paint事件吗?我想我明白了,但点似乎仍然没有移动,甚至没有从对象的位置开始? 什么是汽车类?你可以分享你的整个项目来帮助你。 汽车类只有一个构造函数和析构函数,并从车辆类继承——目前它有点毫无意义,但它只是为了稍后当我添加不同类型的继承自车辆类别。我会将它添加到主要问题中。 @JonathanParkes 对我来说最简单的方法是通过 github、drive、dropbox 或类似方式共享代码以快速查看错误。 是的,我忘记调用继承的构造函数了...但是当我调用驱动函数时它似乎没有更新?以上是关于尝试在 Qt Creator 中绘制,使用对象作为输入的主要内容,如果未能解决你的问题,请参考以下文章
Qt Creator 在尝试运行 OpenCV 程序时崩溃。 [ntdll.dll 崩溃]
在 Windows 上使用 Qt Creator 设置 ccache