#include "car.h"
Car::Car()
{
this->v = 0;
this->t = 0;
}
void Car::step()
{
this->v += this->a * this->t * this->t / 2;
double dt = 0.1;
t += dt;
if(this->v > maxV && a > 0) {
this->t = 0;
a = -a;
}
if(this->v <= 0 && a < 0) {
this->t = 0;
a = -a;
v = 0;
}
}
#ifndef CAR_H
#define CAR_H
class Car
{
public:
Car();
void step();
double getV() {
return v;
}
double getA() {
return a;
}
double getT() {
return t;
}
void setA(double a) {
this->a = a;
}
void setMaxSpeed(double maxV) {
this->maxV = maxV;
}
private:
double a;
double v;
double maxV;
double t;
};
#endif // CAR_H
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
You can make your code fail to compile if it uses deprecated APIs.
In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES +=
car.cpp
main.cpp
mainwindowcar.cpp
HEADERS +=
car.h
mainwindowcar.h
FORMS +=
mainwindowcar.ui
Default rules for deployment.
qnx: target.path = /tmp/TARGET/binelse:unix:!android:target.path=/opt/{TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES +=
mine.qrc
#include "mainwindowcar.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindowCar w;
w.show();
return a.exec();
}
#include "mainwindowcar.h"
#include "ui_mainwindowcar.h"
#include
#include
#include
#include
#include
#include
#include
MainWindowCar::MainWindowCar(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindowCar)
{
ui->setupUi(this);
this->setupImages();
car = new Car();
this->carOneOrTwo = true;
}
MainWindowCar::~MainWindowCar()
{
delete ui;
delete car;
}
void MainWindowCar::setupImages() {
QPixmap p (":/Img/road.png");
if(!ui->Background->scene()) {
QGraphicsScene * s = new QGraphicsScene(this);
ui->Background->setScene(s);
}
ui->Background->scene()->addPixmap(p);
//preload car images
this->carIcon1 = QIcon(":/Img/mypolice.png");
this->carIcon2 = QIcon(":/Img/mypolice2.png");
this->updateCar();
}
void MainWindowCar::updateCar() {
//code to substitute image of a car
//each run to change image to another
if (this->carOneOrTwo)
ui->PushCar->setIcon(this->carIcon1);
else
ui->PushCar->setIcon(this->carIcon2);
carOneOrTwo = !carOneOrTwo;
ui->PushCar->setIconSize(QSize(100, 200));
}
void MainWindowCar::stepUi() {
QRect oldRect = ui->PushCar->geometry();
if(oldRect.x() > this->geometry().width() - 200) {
oldRect.setX(50);
} else {
oldRect.setX(oldRect.x() + (int)car->getV());
}
oldRect.setWidth(120);
ui->PushCar->setGeometry(oldRect);
}
void MainWindowCar::on_pushButtonStart_clicked()
{
//this->ui->pushButtonStart->setVisible(false);
int frame = 0;
bool ok = true;
this->car->setA(this->ui->lineEditAccel->text().toDouble(&ok));
if (!ok) {
QMessageBox::warning(this, "Error", "Please enter a good number for acceleration!");
return;
}
ok = true;
this->car->setMaxSpeed(this->ui->lineEditMaxSpeed->text().toDouble(&ok));
if (!ok) {
QMessageBox::warning(this, "Error", "Please enter a good number for max speed!");
return;
}
this->isStopped = false;
this->ui->pushButtonStart->setEnabled(false);
this->ui->lineEditAccel->setEnabled(false);
this->ui->lineEditMaxSpeed->setEnabled(false);
while(!this->isStopped) {
QCoreApplication::processEvents();
QThread::currentThread()->msleep(100);
this->stepUi();
if (frame % 5 == 0) this->updateCar(); //update each 5th frame
car->step();
qDebug() << car->getV();
frame++;
}
}
void MainWindowCar::on_pushButtonStop_clicked()
{
this->isStopped = true;
this->ui->pushButtonStart->setEnabled(true);
this->ui->lineEditAccel->setEnabled(true);
this->ui->lineEditMaxSpeed->setEnabled(true);
}
#ifndef MAINWINDOWCAR_H
#define MAINWINDOWCAR_H
#include
#include
#include "car.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindowCar; }
QT_END_NAMESPACE
class MainWindowCar : public QMainWindow
{
Q_OBJECT
public:
MainWindowCar(QWidget *parent = nullptr);
~MainWindowCar();
private slots:
void on_pushButtonStart_clicked();
void on_pushButtonStop_clicked();
private:
Ui::MainWindowCar *ui;
public:
Car *car;
QIcon carIcon1;
QIcon carIcon2;
void stepUi();
void setupImages();
void updateCar();
bool isStopped;
bool carOneOrTwo;
};
#endif // MAINWINDOWCAR_H